Python 3.x urllib.request.urlretrieve是否使用代理?
不知何故,我无法通过proxyserver下载文件,我不知道我做错了什么。我只是暂停一下。有什么建议吗Python 3.x urllib.request.urlretrieve是否使用代理?,python-3.x,urllib,Python 3.x,Urllib,不知何故,我无法通过proxyserver下载文件,我不知道我做错了什么。我只是暂停一下。有什么建议吗 import urllib.request urllib.request.ProxyHandler({"http" : "myproxy:123"}) urllib.request.urlretrieve("http://myfile", "file.file") 您需要使用代理对象,而不仅仅是实例化它(您创建了一个对象,但没有将其分配给变量,因此无法使用它)。尝试使用以下模式: #cre
import urllib.request
urllib.request.ProxyHandler({"http" : "myproxy:123"})
urllib.request.urlretrieve("http://myfile", "file.file")
您需要使用代理对象,而不仅仅是实例化它(您创建了一个对象,但没有将其分配给变量,因此无法使用它)。尝试使用以下模式:
#create the object, assign it to a variable
proxy = urllib.request.ProxyHandler({'http': '127.0.0.1'})
# construct a new opener using your proxy settings
opener = urllib.request.build_opener(proxy)
# install the openen on the module-level
urllib.request.install_opener(opener)
# make a request
urllib.request.urlretrieve('http://www.google.com')
或者,如果您不需要依赖std库,请使用请求(此代码来自官方文档):
urllib从系统环境读取代理设置 根据urllib\request.py中的代码片段,只需将http\u proxy和https\u proxy设置为环境变量 同时,这里还记录了:
#代理处理
def getproxies_环境():
“”“返回方案->代理服务器URL映射的字典。
扫描环境中名为_proxy的变量;
这似乎是标准惯例。如果你需要
通过不同的方式,可以将代理字典传递给
[Fancy]URLopener构造函数。
"""
代理={}
#为了更喜欢小写变量,请在
#两遍:第一遍匹配任意一遍,第二遍仅匹配小写
os.environ.items()中的“名称”和“值”:
name=name.lower()
如果值和名称[-6:][='\u proxy':
代理[名称[:-6]]=值
#CVE-2016-1000110-如果我们以CGI脚本的形式运行,请忘记HTTP\U代理
#(非全部小写),因为它可以通过“代理”从web服务器设置:
#来自客户端的标头
#如果“proxy”是小写的,由于下一个块,它仍然会被使用
如果操作系统环境中有“请求方法”:
proxies.pop('http',无)
os.environ.items()中的“名称”和“值”:
如果名称[-6:][='\u代理':
name=name.lower()
如果值:
代理[名称[:-6]]=值
其他:
proxies.pop(名称[:-6],无)
返回代理
urllib没有属性请求,应该是urllib2@arbel在Python3中,确实如此@多瓦克:是的!第二个选择对我很有效。谢谢!!在Python3.9中,使用proxy=urllib.request.ProxyHandler({'http':'socks5://127.0.0.1:9050','https':'socks5://127.0.0.1:9050})
我得到一个操作错误:隧道连接失败:501 Tor不是http代理
import requests
proxies = {"http": "http://10.10.1.10:3128",
"https": "http://10.10.1.10:1080"}
requests.get("http://example.org", proxies=proxies)
# Proxy handling
def getproxies_environment():
"""Return a dictionary of scheme -> proxy server URL mappings.
Scan the environment for variables named <scheme>_proxy;
this seems to be the standard convention. If you need a
different way, you can pass a proxies dictionary to the
[Fancy]URLopener constructor.
"""
proxies = {}
# in order to prefer lowercase variables, process environment in
# two passes: first matches any, second pass matches lowercase only
for name, value in os.environ.items():
name = name.lower()
if value and name[-6:] == '_proxy':
proxies[name[:-6]] = value
# CVE-2016-1000110 - If we are running as CGI script, forget HTTP_PROXY
# (non-all-lowercase) as it may be set from the web server by a "Proxy:"
# header from the client
# If "proxy" is lowercase, it will still be used thanks to the next block
if 'REQUEST_METHOD' in os.environ:
proxies.pop('http', None)
for name, value in os.environ.items():
if name[-6:] == '_proxy':
name = name.lower()
if value:
proxies[name[:-6]] = value
else:
proxies.pop(name[:-6], None)
return proxies