Sometimes, we want to use proxy with Python urllib2.
In this article, we’ll look at how to use proxy with Python urllib2.
How to use proxy with Python urllib2?
To use proxy with Python urllib2, we can use the ProxyHandler
class.
For instance, we write
proxy = urllib2.ProxyHandler({'http': '127.0.0.1'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
urllib2.urlopen('http://www.example.com')
to create a ProxyHandler
object.
Then we call build_opened
with proxy
to use the proxy.
And then we call install_opener
with opener
to use the opener.
Finally, we call urlopen
with the URL to make a GET request via the proxy.
Conclusion
To use proxy with Python urllib2, we can use the ProxyHandler
class.