定义 :
urllib2.ProxyHandler([proxies])
导致请求通过代理。如果给出了代理,它必须是一个将协议名称映射到代理 URL 的字典。默认是从环境变量 _proxy 中读取代理列表。如果没有设置代理环境变量,则在 Windows 环境中,从注册表的 Internet 设置部分获取代理设置,在 Mac OS X 环境中,从 OS X 系统配置框架检索代理信息。
我的理解是,如果没有明确设置代理,它会从注册表设置中检测到代理。
当我运行以下代码时:
import urllib2
proxy_support = urllib2.ProxyHandler({})
print "1"
opener = urllib2.build_opener(proxy_support)
print "2"
urllib2.install_opener(opener)
print "3"
response = urllib2.urlopen('http://google.com')
print "4"
html = response.read()
我得到错误:
1
2
3
urllib2.URLError: <urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>
这意味着这段代码无法打开网站。我不确定我哪里出错了,不应该按照定义,urllib2.ProxyHandler
从注册表中删除代理,因为我没有明确设置代理?