ftp.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- test-case-name: twisted.test.test_ftp_options -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. I am the support module for making a ftp server with twistd.
  6. """
  7. import warnings
  8. from twisted.application import internet
  9. from twisted.cred import checkers, portal, strcred
  10. from twisted.protocols import ftp
  11. from twisted.python import deprecate, usage, versions
  12. class Options(usage.Options, strcred.AuthOptionMixin):
  13. synopsis = """[options].
  14. WARNING: This FTP server is probably INSECURE do not use it.
  15. """
  16. optParameters = [
  17. ["port", "p", "2121", "set the port number"],
  18. ["root", "r", "/usr/local/ftp", "define the root of the ftp-site."],
  19. ["userAnonymous", "", "anonymous", "Name of the anonymous user."],
  20. ]
  21. compData = usage.Completions(
  22. optActions={"root": usage.CompleteDirs(descr="root of the ftp site")}
  23. )
  24. longdesc = ""
  25. def __init__(self, *a, **kw):
  26. usage.Options.__init__(self, *a, **kw)
  27. self.addChecker(checkers.AllowAnonymousAccess())
  28. def opt_password_file(self, filename):
  29. """
  30. Specify a file containing username:password login info for
  31. authenticated connections. (DEPRECATED; see --help-auth instead)
  32. """
  33. self["password-file"] = filename
  34. msg = deprecate.getDeprecationWarningString(
  35. self.opt_password_file, versions.Version("Twisted", 11, 1, 0)
  36. )
  37. warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
  38. self.addChecker(checkers.FilePasswordDB(filename, cache=True))
  39. def makeService(config):
  40. f = ftp.FTPFactory()
  41. r = ftp.FTPRealm(config["root"])
  42. p = portal.Portal(r, config.get("credCheckers", []))
  43. f.tld = config["root"]
  44. f.userAnonymous = config["userAnonymous"]
  45. f.portal = p
  46. f.protocol = ftp.FTP
  47. try:
  48. portno = int(config["port"])
  49. except KeyError:
  50. portno = 2121
  51. return internet.TCPServer(portno, f)