Python ftplib - specify port

前端 未结 4 1314
野性不改
野性不改 2020-12-16 11:36

I would like to specify the port with Python\'s ftplib client (instead of default port 21).

Here is the code:

from ftplib import FTP
ftp = FTP(\'loc         


        
相关标签:
4条回答
  • 2020-12-16 11:41
    >>> from ftplib import FTP
    >>> HOST = "localhost"
    >>> PORT = 12345 # Set your desired port number
    >>> ftp = FTP()
    >>> ftp.connect(HOST, PORT)
    
    0 讨论(0)
  • 2020-12-16 11:44

    Found the answer. Instantiate the FTP object and then run connect on it like so:

    from ftplib import FTP
    ftp = FTP()
    ftp.connect('localhost', 2121)
    
    0 讨论(0)
  • 2020-12-16 11:52

    Yes you can use connect

    from ftplib import FTP
    
    my_ftp = FTP()
    my_ftp.connect('localhost', 80) # 80 is the port for example
    
    0 讨论(0)
  • 2020-12-16 11:55

    After searching numerous solutions, a combination of the docs.python.org and the connect command solved my issue.

    from ftplib import FTP_TLS
    
    host = 'host'
    port = 12345
    usr = 'user'
    pwd = 'password'
    ftps = FTP_TLS()
    ftps.connect(host, port)
    # Output: '220 Server ready for new user.'
    ftps.login(usr, pwd)
    # Output: '230 User usr logged in.'
    ftps.prot_p()
    # Output: '200 PROT command successful.'
    ftp.nlst()
    # Output: ['mysubdirectory', 'mydoc']
    

    If you're using plain FTP instead of FTPS, just use ftplib.FTP instead.

    0 讨论(0)
提交回复
热议问题