我正在使用 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')
关于python - urllib.urlretrieve 的代理设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33390610/