pyftp.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python3
  2. """
  3. pip install pyftpdlib
  4. """
  5. import argparse
  6. import os
  7. from os import path
  8. from pyftpdlib.handlers import FTPHandler
  9. from pyftpdlib.servers import FTPServer
  10. from pyftpdlib.authorizers import DummyAuthorizer
  11. handler = FTPHandler
  12. def init_auth(dir):
  13. authorizer = DummyAuthorizer()
  14. authorizer.add_user('admin', '123456', dir, perm='elradfmwM')
  15. handler.authorizer = authorizer
  16. if __name__ == "__main__":
  17. parser = argparse.ArgumentParser(description='ftp module of argparse')
  18. # 指定-p/--port的参数
  19. # 类型为int
  20. # help为简短地说明
  21. parser.add_argument(
  22. '-p', '--port', default=2121, type=int,
  23. help='ftp server port'
  24. )
  25. # 指定-H/--host
  26. parser.add_argument(
  27. '-H', '--host', default="localhost", type=str,
  28. help='ftp server bind host'
  29. )
  30. # 指定-d/--dir参数
  31. parser.add_argument(
  32. '-d', '--dir', default=".", type=str,
  33. help='ftp server user home'
  34. )
  35. args = parser.parse_args()
  36. dir = path.abspath(args.dir)
  37. print("current dir: {}", dir)
  38. init_auth(dir)
  39. server = FTPServer((args.host, args.port), handler)
  40. print("ftp server start at: {}:{}".format(args.host, args.port))
  41. server.serve_forever()