-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.py
More file actions
64 lines (52 loc) · 1.66 KB
/
Copy pathservices.py
File metadata and controls
64 lines (52 loc) · 1.66 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
from pyftpdlib import servers
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
class FTPServer:
def __init__(self, user, password, port, path):
self.user = user
self.password = password
self.address = ('0.0.0.0', port)
self.handler = FTPHandler
self.path = path or self.get_working_dir(None)
self.set_credentials()
self.server = servers.FTPServer(self.address, FTPHandler)
@staticmethod
def get_working_dir(path):
if path:
return path
import pathlib
path = pathlib.Path(__file__).parent.resolve()
path = path / 'data'
path.mkdir(exist_ok=True)
return str(path)
def set_credentials(self):
authorizer = DummyAuthorizer()
authorizer.add_user(self.user, self.password, self.path, perm="elradfmwMT")
self.handler.authorizer = authorizer
def start(self):
self.server.serve_forever()
def stop(self):
self.server.close_all()
@staticmethod
def get_ip():
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
return IP
class FTPManager:
@staticmethod
def start(*args, **kwargs):
FTPServer(*args, **kwargs).start()
@staticmethod
def get_ip():
return FTPServer.get_ip()
@staticmethod
def get_path(path):
return FTPServer.get_working_dir(path)