-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftpd.py
53 lines (40 loc) · 1.63 KB
/
ftpd.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
#!/usr/bin/env python
from os import environ
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
def main():
PORT = int(environ['FTP_PORT']) if 'FTP_PORT' in environ else 21
MIN_PORT = 40000
MAX_PORT = 65535
ROOT = environ['FTP_ROOT'] if 'FTP_ROOT' in environ else '/var/ftp'
if 'PASV_PORTS' in environ:
ports = [int(p) for p in environ['PASV_PORTS'].split('-')]
MIN_PORT, MAX_PORT = ports[0], ports[1]
# Instantiate a dummy authorizer for managing 'virtual' users
authorizer = DummyAuthorizer()
# Define a new user having full r/w permissions and a read-only
# anonymous user
authorizer.add_user(environ['FTP_USER'],
environ['FTP_PASS'],
ROOT, perm='elradfmwM')
# Instantiate FTP handler class
handler = FTPHandler
handler.authorizer = authorizer
# Define a customized banner (string returned when client connects)
handler.banner = "pyftpdlib based ftpd ready."
# Specify a masquerade address and the range of ports to use for
# passive connections. Decomment in case you're behind a NAT.
if 'PASV_ADDRESS' in environ:
handler.masquerade_address = environ['PASV_ADDRESS']
handler.passive_ports = range(MIN_PORT, MAX_PORT)
# Instantiate FTP server class and listen on 0.0.0.0:21
address = ('', PORT)
server = FTPServer(address, handler)
# set a limit for connections
server.max_cons = 256
server.max_cons_per_ip = 5
# start ftp server
server.serve_forever()
if __name__ == '__main__':
main()