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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34576665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:12:19  来源:igfitidea点击:

Setting proxy to urllib.request (Python3)

pythonproxyurllib

提问by gm1

How can I set proxy for the last urllibin Python 3. I am doing the next

如何urllib在 Python 3 中为最后一个设置代理。我正在做下一个

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()

I tried adding proxy as follows :

我尝试添加代理如下:

ask=urlrequest.Request.set_proxy(ask,proxies,'http')

However I don't know how correct it is since I am getting the next error:

但是我不知道它有多正确,因为我收到下一个错误:

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'

采纳答案by mhawke

You should be calling set_proxy()on an instanceof class Request, not on the class itself:

您应该调用classset_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'))

回答by Alexander Taubenkorb

I needed to disable the proxyin our company environment, because I wanted to access a server on localhost. I could not disable the proxy server with the approach from @mhawke (tried to pass {}, Noneand []as proxies).

我需要在我们公司的环境中禁用代理,因为我想访问本地主机上的服务器。我无法使用@mhawke 的方法禁用代理服务器(试图通过{}None[]作为代理)。

This worked for me (can also be used for setting a specific proxy, see comment in code).

这对我有用(也可用于设置特定代理,请参阅代码中的注释)。

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)

回答by CONvid19

I normally use the following code for proxy requests:

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

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'])

回答by Pierz

Urllib will automatically detect proxiesset up in the environment - so one can just set the HTTP_PROXYvariable either in your environment e.g. for Bash:

Urllib 将自动检测在环境中设置的代理- 因此HTTP_PROXY您可以在您的环境中设置变量,例如用于 Bash:

export HTTP_PROXY=http://proxy_url:proxy_port

or using Python e.g.

或使用 Python 例如

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