FTPModule.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import os
  2. from pyftpdlib.authorizers import DummyAuthorizer
  3. from pyftpdlib.handlers import FTPHandler
  4. from pyftpdlib.servers import FTPServer
  5. import requests
  6. import config
  7. from SysInfo import Informator
  8. from ftplib import FTP
  9. informator = Informator()
  10. class MyHandler(FTPHandler):
  11. def on_connect(self):
  12. print("Client connected")
  13. def on_disconnect(self):
  14. print("Client disconnected")
  15. def on_login(self, username):
  16. print("User "+username+" has been connected")
  17. pass
  18. def on_logout(self, username):
  19. print("User "+username+" has been disconnected")
  20. def on_file_sent(self, file):
  21. # do something when a file has been sent
  22. pass
  23. def on_file_received(self, file):
  24. nodes = str(requests.get(f"{config.ip}/nodes_list").text).split(",")
  25. for node in nodes:
  26. if node==str(informator.ip):
  27. continue
  28. else:
  29. ftp = FTP()
  30. ftp.connect(node)
  31. ftp.login("user", "12345")
  32. with open(file, "rb") as f:
  33. # Command for Uploading the file "STOR filename"
  34. ftp.storbinary(f"STOR {file}", f)
  35. ftp.dir()
  36. ftp.close()
  37. pass
  38. def on_incomplete_file_sent(self, file):
  39. # do something when a file is partially sent
  40. pass
  41. def on_incomplete_file_received(self, file):
  42. # remove partially uploaded files
  43. import os
  44. os.remove(file)
  45. #send msg of error
  46. class FTPS:
  47. def __init__(self,username,password,path):
  48. self.username = username
  49. self.password = password
  50. self.path = path
  51. def start_server(self,address):
  52. authorizer = DummyAuthorizer()
  53. authorizer.add_user(self.username,self.password,self.path, perm='elradfmwMT')
  54. authorizer.add_anonymous(os.getcwd())
  55. handler = MyHandler
  56. handler.authorizer = authorizer
  57. handler.banner = "pyftpdlib based ftpd ready."
  58. server = FTPServer(address, handler)
  59. server.max_cons = 256
  60. server.max_cons_per_ip = 5
  61. server.serve_forever()