sultan
sultan

Reputation: 6058

Python: urllib2 handle multiple openers

I have to keep 2 urllib2 openers, one for direct requests and the second to make requests via proxy server and I've to rebuild opener switch between requests.

How to keep context openers for example direct and proxy separately?

Upvotes: 1

Views: 1905

Answers (1)

itsadok
itsadok

Reputation: 29342

I suspect your confusion stems from using install_opener and urllib2.urlopen. Instead, just call build_opener twice and store the results in separate objects. Then you can use the appropriate opener when needed.

Example:

import urllib2
direct = urllib2.build_opener()
proxy_handler = urllib2.ProxyHandler({'http': 'http://www.example.com:3128/'})
proxied = urllib2.build_opener(proxy_handler)


direct.open('http://stackoverflow.com') # opens directly
proxied.open('http://stackoverflow.com') # opens through proxy

Upvotes: 6

Related Questions