在 Python 中通过 FTP 代理连接 ftplib?
Connecting with ftplib via FTP proxy in Python?
我正在尝试从 FTP 下载文件。它在家里工作正常,但当我 运行 通过公司网络时它不起作用。我知道这与代理有关。我在 Python 中查看了一些关于代理问题的帖子。我试图建立与代理的连接。它在 url 上工作正常,但在连接到 FTP 时失败。有谁知道这样做的方法吗?提前致谢。
下面是我的代码:
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 # and socket type values to enum constants.
731 addrlist = []
--> 732 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
733 af, socktype, proto, canonname, sa = res
734 addrlist.append((_intenum_converter(af, AddressFamily),
gaierror: [Errno 11004] getaddrinfo failed
我可以使用 FileZilla 通过代理连接,方法是选择自定义 FTP 代理,规格为:
USER %u@%h %s
PASS %p
ACCT %w
FTP Proxy using FileZilla
您正在使用 FTP 代理进行连接。
FTP 代理不能与 HTTP 一起工作,所以你针对 http://
URL 到 www.google.com
的测试是完全不相关的,不能证明任何事情。
FTP 代理作为 FTP 服务器工作。您连接到代理,而不是实际的服务器。然后使用用户名(或其他凭据)的一些特殊语法来指定您的实际目标 FTP 服务器及其凭据。在您的情况下,用户名的特殊语法是 user@host user_proxy
。您的代理需要 FTP ACCT
命令中的代理密码。
这应该适用于您的具体情况:
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)
我正在尝试从 FTP 下载文件。它在家里工作正常,但当我 运行 通过公司网络时它不起作用。我知道这与代理有关。我在 Python 中查看了一些关于代理问题的帖子。我试图建立与代理的连接。它在 url 上工作正常,但在连接到 FTP 时失败。有谁知道这样做的方法吗?提前致谢。
下面是我的代码:
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 # and socket type values to enum constants.
731 addrlist = []
--> 732 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
733 af, socktype, proto, canonname, sa = res
734 addrlist.append((_intenum_converter(af, AddressFamily),
gaierror: [Errno 11004] getaddrinfo failed
我可以使用 FileZilla 通过代理连接,方法是选择自定义 FTP 代理,规格为:
USER %u@%h %s
PASS %p
ACCT %w
FTP Proxy using FileZilla
您正在使用 FTP 代理进行连接。
FTP 代理不能与 HTTP 一起工作,所以你针对 http://
URL 到 www.google.com
的测试是完全不相关的,不能证明任何事情。
FTP 代理作为 FTP 服务器工作。您连接到代理,而不是实际的服务器。然后使用用户名(或其他凭据)的一些特殊语法来指定您的实际目标 FTP 服务器及其凭据。在您的情况下,用户名的特殊语法是 user@host user_proxy
。您的代理需要 FTP ACCT
命令中的代理密码。
这应该适用于您的具体情况:
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)