-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (60 loc) · 2.6 KB
/
Copy pathmain.py
File metadata and controls
78 lines (60 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# The main CLI entrypoint for thinFTP Server
#
# This script parses command-line arguments and starts the thinFTP server.
import argparse
import getpass
import sys
import traceback
from thinftp.logger import get_logger
from thinftp.server import start_server
from thinftp import __version__ as tftp_version
def main():
"""Main entry point for the thinftp CLI."""
# Parse command-line arguments
parser = argparse.ArgumentParser(prog="thinftp",
description="A simple FTP server for testing purposes",
epilog="Examples:\n"
" thinftp -h localhost -p 2121 -u testuser -d ./testdir\n"
" thinftp --bind localhost --port 2121 --user testuser --directory ./testdir")
parser.add_argument('-v', '--version',
action="version",
version=f"%(prog)s {tftp_version}")
parser.add_argument('-b', '--bind',
default="0.0.0.0",
help="Binds the IP address of the server (default: %(default)s)")
parser.add_argument('-p', '--port',
default=2528,
type=int,
help="Sets the Port of the server (default: %(default)s)")
parser.add_argument('-u', '--user',
default=getpass.getuser(),
help="Sets the Username for the server")
parser.add_argument('-d', '--directory',
default=".",
help="Sets the root Directory (default: %(default)s)")
parser.add_argument('-D', '--debug',
action='store_true',
help="Enable DEBUG logs")
opts = parser.parse_args()
# Get the password from the user
opts.pswd = getpass.getpass(f"Set Password for {opts.user}: ")
# Get the logger instance
opts.lgr = get_logger(debug=opts.debug)
# Log the startup message
opts.lgr.info("Welcome to thinFTP server")
try:
# Start the server
start_server(opts)
except Exception as e:
# Handle any unhandled exceptions
if opts.debug:
# Reraise the exception if --debug is specified
raise
else:
# Log the exception if --debug is not specified
tb = ''.join(traceback.format_exception(type(e), e, e.__traceback__))
opts.lgr.critical("Unhandled exception: \n"+ tb)
# Exit with a non-zero status code
sys.exit(1)
if __name__ == "__main__":
main()