将代理设置为urlib.request(Python3)

时间:2016-01-03 12:45:56

标签: python proxy urllib

如何为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'

4 个答案:

答案 0 :(得分:9)

您应该在类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'))

答案 1 :(得分:2)

我需要在公司环境中禁用代理,因为我想访问localhost上的服务器。我无法使用@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)

答案 2 :(得分:0)

Urllib will automatically detect proxies在环境中设置-因此,您可以只在环境中设置HTTP_PROXY变量,例如对于Bash:

export HTTP_PROXY=http://proxy_url:proxy_port

或使用Python例如

import os
os.environ['HTTP_PROXY'] = 'http://proxy_url:proxy_port'

答案 3 :(得分:-1)

我通常使用以下代码进行代理请求:

import requests
proxies = {
    'http': 'http://proxy.server:port',
    'https': 'http://proxyserver:port',
}
s = requests.Session()
s.proxies = proxies
r = s.get('https://api.ipify.org?format=json').json()
print(r['ip'])