-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.py
81 lines (72 loc) · 3.24 KB
/
server.py
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
78
79
80
81
#
# MIT License
#
# Copyright (c) 2020 Denis Kovalchuk
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import sys
import logging
import argparse
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.handlers import TLS_FTPHandler
from pyftpdlib.servers import FTPServer
def main():
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('root_directory')
arg_parser.add_argument('port')
arg_parser.add_argument('--use-ssl', choices = ['yes', 'no'], default = 'no')
args = arg_parser.parse_args()
# Add users with the following permissions:
# e - change directory (CWD, CDUP commands)
# l - list files (LIST, NLST, STAT, MLSD, MLST, SIZE commands)
# r - retrieve file from the server (RETR command)
# a - append data to an existing file (APPE command)
# d - delete file or directory (DELE, RMD commands)
# f - rename file or directory (RNFR, RNTO commands)
# m - create directory (MKD command)
# w - store a file to the server (STOR, STOU commands)
# M - change file mode / permission (SITE CHMOD command)
authorizer = DummyAuthorizer()
authorizer.add_user("user", "password", args.root_directory, perm = "elradfmwM")
authorizer.add_user("alice", "password", args.root_directory, perm = "elradfmwM")
if args.use_ssl == 'yes':
handler = TLS_FTPHandler
handler.certfile = os.path.join(sys.path[0], 'certs/server_cert.pem')
handler.keyfile = os.path.join(sys.path[0], 'certs/server_cert.key')
handler.tls_control_required = True
handler.tls_data_required = True
log_filename = "ssl_server.log"
else:
handler = FTPHandler
log_filename = "server.log"
handler.authorizer = authorizer
handler.banner = "FTP server is ready."
if os.path.exists(log_filename):
os.remove(log_filename)
logging.basicConfig(handlers = [
logging.FileHandler(log_filename),
logging.StreamHandler()],
level = logging.DEBUG)
server = FTPServer(("127.0.0.1", args.port), handler)
server = FTPServer(("::1", args.port), handler)
server.serve_forever()
if __name__ == "__main__":
main()