Proxy with urllib2 Proxy with urllib2 python python

Proxy with urllib2


proxy = urllib2.ProxyHandler({'http': '127.0.0.1'})opener = urllib2.build_opener(proxy)urllib2.install_opener(opener)urllib2.urlopen('http://www.google.com')


You have to install a ProxyHandler

urllib2.install_opener(    urllib2.build_opener(        urllib2.ProxyHandler({'http': '127.0.0.1'})    ))urllib2.urlopen('http://www.google.com')


You can set proxies using environment variables.

import osos.environ['http_proxy'] = '127.0.0.1'os.environ['https_proxy'] = '127.0.0.1'

urllib2 will add proxy handlers automatically this way. You need to set proxies for different protocols separately otherwise they will fail (in terms of not going through proxy), see below.

For example:

proxy = urllib2.ProxyHandler({'http': '127.0.0.1'})opener = urllib2.build_opener(proxy)urllib2.install_opener(opener)urllib2.urlopen('http://www.google.com')# next line will fail (will not go through the proxy) (https)urllib2.urlopen('https://www.google.com')

Instead

proxy = urllib2.ProxyHandler({    'http': '127.0.0.1',    'https': '127.0.0.1'})opener = urllib2.build_opener(proxy)urllib2.install_opener(opener)# this way both http and https requests go through the proxyurllib2.urlopen('http://www.google.com')urllib2.urlopen('https://www.google.com')