问题描述
我的代码belove给我错误:socket.gaierror: [Errno 11001] getaddrinfo failed
调用方法ftp.connect()时.
My code belove gives me the error: socket.gaierror: [Errno 11001] getaddrinfo failed
when calling the method ftp.connect().
我的问题是:为什么我可以连接到google.com,但是连接到ftp服务器时却出现错误?以及如何从http代理后面连接到ftp服务器?
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()
推荐答案
您可以尝试将FTP网址直接发送到HTTP代理.
You may try to send the FTP url directly to the HTTP proxy.
f = urllib.request.urlopen('ftp://abc:xyz@host/my-file.ext')
还要检查其他FTP客户端或浏览器是否成功使用FTP服务器.如果是这样,请使用 Wireshark 之类的网络嗅探器,观察它们如何与FTP服务器通信.
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.
这篇关于通过http代理连接到FTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!