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 con开发者_StackOverflow中文版text openers for example direct and proxy separately?
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
精彩评论