Python SSL FTP connection timing out

陌路散爱 提交于 2021-02-08 06:58:46

问题


I'm trying to get a connection established to a FTP server with SSL from within Python (v3.3.0). But I keep getting a timeout. I am NOT using port 990 as the SSL port (paranoid). Would that be the cause of this problem? And if so, how do I specify the port I am using?

Here's my script:

    from ftplib import FTP
    from ftplib import FTP_TLS

    ftps = FTP_TLS('ip address')

    ftps.auth()

    ftps.sendcmd('USER uname') 
    ftps.sendcmd('PASS password')

    ftps.prot_p()
    ftps.retrlines('LIST')

    ftps.close()

And here is the result:

Traceback (most recent call last):
  File "Scrpit name removed for posting", line 12, in <module>
    ftps.retrlines('LIST')
  File "C:\Python33\lib\ftplib.py", line 767, in retrlines
    conn = self.transfercmd(cmd)
  File "C:\Python33\lib\ftplib.py", line 381, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "C:\Python33\lib\ftplib.py", line 742, in ntransfercmd
    conn, size = FTP.ntransfercmd(self, cmd, rest)
  File "C:\Python33\lib\ftplib.py", line 343, in ntransfercmd
    source_address=self.source_address)
  File "C:\Python33\lib\socket.py", line 424, in create_connection
    raise err
  File "C:\Python33\lib\socket.py", line 415, in create_connection
   sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

any advice would be greatly appreciated,


回答1:


After looking at the ftplib source, it doesn't seem to want to use any port but 21.

I think you should be able to work around this, something like

import ftplib

ftplib.FTP.port = 995     # or whatever port you are using
ftps = ftplib.FTP_TLS('hostname', 'user', 'pwd')
ftps.retrlines('LIST')



回答2:


Set the port through the connect

import ftplib

ftps = ftplib.FTP_TLS()
ftps.connect ('hostname', 991)


来源:https://stackoverflow.com/questions/20152504/python-ssl-ftp-connection-timing-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!