在Python中通过FTP代理连接ftplib?
我正在尝试从FTP下载文件。它在家里工作很好,但在我浏览公司网络时就不起作用了。我知道这和代理有关。我已经看了一些关于Python中代理问题的帖子。我已尝试建立到代理的连接。url正常工作,但连接到FTP时失败。有人知道怎么做吗?提前谢谢 下面是我的代码:在Python中通过FTP代理连接ftplib?,python,python-3.x,proxy,ftp,ftplib,Python,Python 3.x,Proxy,Ftp,Ftplib,我正在尝试从FTP下载文件。它在家里工作很好,但在我浏览公司网络时就不起作用了。我知道这和代理有关。我已经看了一些关于Python中代理问题的帖子。我已尝试建立到代理的连接。url正常工作,但连接到FTP时失败。有人知道怎么做吗?提前谢谢 下面是我的代码: import os import urllib import ftplib from ftplib import FTP from getpass import getpass from urllib.request import urlop
import os
import urllib
import ftplib
from ftplib import FTP
from getpass import getpass
from urllib.request import urlopen, ProxyHandler, HTTPHandler, HTTPBasicAuthHandler, \
build_opener, install_opener
user_proxy = "XXX"
pass_proxy = "YYY"
url_proxy = "ZZZ"
port_proxy = "89"
url_proxy = "ftp://%s:%s@%s:%s" % (user_proxy, pass_proxy, url_proxy, port_proxy)
authinfo = urllib.request.HTTPBasicAuthHandler()
proxy_support = urllib.request.ProxyHandler({"ftp" : url_proxy})
# build a new opener that adds authentication and caching FTP handlers
opener = urllib.request.build_opener(proxy_support, authinfo,
urllib.request.CacheFTPHandler)
# install it
urllib.request.install_opener(opener)
#url works ok
f = urllib.request.urlopen('http://www.google.com/')
print(f.read(500))
urllib.request.install_opener(opener)
#ftp is not working
ftp = ftplib.FTP('ftp:/ba1.geog.umd.edu', 'user', 'burnt_data')
我收到的错误消息是:
730#和套接字类型值,以枚举常量。
731地址列表=[]
-->732对于_socket.getaddrinfo(主机、端口、系列、类型、协议、标志)中的res:
733 af,socktype,原型,canonname,sa=res
734 addrlist.append(_intenum_转换器(af,AddressFamily),
gaierror:[Errno 11004]getaddrinfo失败
我可以使用FileZilla通过代理进行连接,方法是选择具有以下规格的自定义FTP代理:
用户%u@%h%s
通过%p
会计科目%w
您正在使用FTP代理进行连接 FTP代理无法与HTTP一起工作,因此您对
HTTP://
URL到www.google.com
的测试完全不相关,不能证明任何事情
FTP代理用作FTP服务器。您连接到代理,而不是实际的服务器。然后使用用户名(或其他凭据)的一些特殊语法指定实际目标FTP服务器及其凭据。在您的情况下,用户名的特殊语法为user@host用户\u proxy
。您的代理需要FTPACCT
命令中的代理密码
这应该适用于您的具体情况:
host_proxy = '192.168.149.50'
user_proxy = 'XXX'
pass_proxy = 'YYY'
user = 'user'
user_pass = 'burnt_data'
host = 'ba1.geog.umd.edu'
u = "%s@%s %s" % (user, host, user_proxy)
ftp = ftplib.FTP(host_proxy, u, user_pass, pass_proxy)
不需要其他代码(urllib
或任何其他代码)
如果代理使用自定义端口(不是21),请使用以下选项:
ftp = ftplib.FTP()
ftp.connect(host_proxy, port_proxy)
ftp.login(u, user_pass, pass_proxy)