Setting proxy to urllib.request (Python3)

不问归期 提交于 2019-12-04 11:24:06

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

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

I needed to disable the proxy in 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 {}, None and [] as proxies).

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)

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

export HTTP_PROXY=http://proxy_url:proxy_port

or using Python e.g.

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

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'])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!