问题
My code belove gives me the error: socket.gaierror: [Errno 11001] getaddrinfo failed
when calling the method ftp.connect().
My question is: why can I connect to google.com but when connecting to an ftp server it gives me error? And how I can connect to the ftp server from behind http proxy?
import ftplib
import urllib.request
# ftp settings
ftpusername = 'abc'
ftppassword = 'xyz'
ftp_host = 'host'
ftp_port = 1234
proxy_url = 'http://username:password@host:port'
proxy_support = urllib.request.ProxyHandler({'http': proxy_url})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
#url works ok
f = urllib.request.urlopen('http://www.google.com/')
print(f.read(500))
# Connecting to ftp fails
with ftplib.FTP() as ftp:
ftp.connect(host=ftp_host, ftp_port=port)
ftp.login(user=ftpusername, passwd=ftppassword)
print(ftp.getwelcome())
print(ftp.nlst())
ftp.close()
ftp.quit()
回答1:
You may try to send the FTP url directly to the HTTP proxy.
f = urllib.request.urlopen('ftp://abc:xyz@host/my-file.ext')
Check also if other FTP clients or browsers succeed in iteracting with the FTP server. If they do, use a network sniffer like Wireshark and observe how they talk to the FTP server.
回答2:
You may use a pasive FTP connection. It may be solves the problem
来源:https://stackoverflow.com/questions/49138118/connect-to-ftp-server-through-http-proxy