我正在使用urllib.urlretrieve以匿名模式在远程FTP站点上下载文件。

ftp://ftp.{DOMAIN}/register/{FOLDER1}/{FOLDER2}/{FOLDER3}/{FILE}

import urllib
urllib.urlretrieve(FILE_TO_DOWNLOAD, DESTINATION_FILE)


当它在我的公司计算机上运行时,出现以下错误:

IOError: [Errno ftp error] proxy support for ftp protocol currently not implemented


我知道代码中缺少代理设置,但是我不知道在哪里以及如何设置代理设置。

最佳答案

如何设置代理:

Python 2.7.10

通过代理传递到urlopen中。请参阅文档here

# Use http://www.someproxy.com:3128 for http proxying
proxies = {'http': 'http://www.someproxy.com:3128'}
filehandle = urllib.urlopen(some_url, proxies=proxies)


或者,在启动python解释器之前设置http_proxy环境变量。

% http_proxy="http://www.someproxy.com:3128"
% export http_proxy
% python


Python 3.5

这是从文档here中正确的。

import urllib.request
proxies = {'http': 'http://proxy.example.com:8080/'}
opener = urllib.request.FancyURLopener(proxies)
with opener.open("http://www.python.org") as f:
f.read().decode('utf-8')

10-07 13:22
查看更多