Connect to FTP server through http proxy

故事扮演 提交于 2019-12-21 22:09:12

问题


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

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