如何在Python 3中为最后一个
urllib
设置代理。
我正在做下一个
from urllib import request as urlrequest
ask = urlrequest.Request(url) # note that here Request has R not r as prev versions
open = urlrequest.urlopen(req)
open.read()
我尝试添加代理,如下所示:
ask=urlrequest.Request.set_proxy(ask,proxies,'http')
但是我不知道它有多正确,因为我收到了下一个错误:
336 def set_proxy(self, host, type):
--> 337 if self.type == 'https' and not self._tunnel_host:
338 self._tunnel_host = self.host
339 else:
AttributeError: 'NoneType' object has no attribute 'type'
您应该在类
set_proxy()
的 实例 上调用 Request
,而不是类本身:
from urllib import request as urlrequest
proxy_host = 'localhost:1234' # host and port of your proxy
url = 'http://www.httpbin.org/ip'
req = urlrequest.Request(url)
req.set_proxy(proxy_host, 'http')
response = urlrequest.urlopen(req)
print(response.read().decode('utf8'))
我需要在我们公司环境中禁用代理,因为我想访问本地主机上的服务器。我无法使用 @mhawke 的方法禁用代理服务器(尝试将
{}
、None
和 []
作为代理传递)。
这对我有用(也可用于设置特定代理,请参阅代码中的注释)。
import urllib.request as request
# disable proxy by passing an empty
proxy_handler = request.ProxyHandler({})
# alertnatively you could set a proxy for http with
# proxy_handler = request.ProxyHandler({'http': 'http://www.example.com:3128/'})
opener = request.build_opener(proxy_handler)
url = 'http://www.example.org'
# open the website with the opener
req = opener.open(url)
data = req.read().decode('utf8')
print(data)
Urllib 将自动检测环境中设置的代理 - 因此人们可以在您的环境中设置
HTTP_PROXY
变量,例如对于 Bash:
export HTTP_PROXY=http://proxy_url:proxy_port
或者,如果您担心污染环境,您可以为每个进程设置变量:
HTTP_PROXY=http://proxy_url:proxy_port your_python_app
或使用Python例如
import os
os.environ['HTTP_PROXY'] = 'http://proxy_url:proxy_port'
urllib 文档中的注释:“如果设置了变量
HTTP_PROXY
,则将忽略REQUEST_METHOD
[环境变量];请参阅有关 getproxies() 的文档”
import urllib.request
def set_http_proxy(proxy):
if proxy == None: # Use system default setting
proxy_support = urllib.request.ProxyHandler()
elif proxy == '': # Don't use any proxy
proxy_support = urllib.request.ProxyHandler({})
else: # Use proxy
proxy_support = urllib.request.ProxyHandler({'http': '%s' % proxy, 'https': '%s' % proxy})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
proxy = 'user:pass@ip:port'
set_http_proxy(proxy)
url = 'https://www.httpbin.org/ip'
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
html = response.read()
html
可以通过像下面的代码这样设置来为 python 代码上的任何请求设置全局代理:
import os
proxy = 'http://<user>:<pass>@<proxy>:<port>'
os.environ['http_proxy'] = proxy
os.environ['HTTP_PROXY'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTPS_PROXY'] = proxy