What's the best way to specify a proxy with username and password for an http connection in python?
相关问题
- Angular RxJS mergeMap types
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
This works for me:
The best way of going through a proxy that requires authentication is using urllib2 to build a custom url opener, then using that to make all the requests you want to go through the proxy. Note in particular, you probably don't want to embed the proxy password in the url or the python source code (unless it's just a quick hack).
In a more complex program, you can seperate these components out as appropriate (for instance, only using one password manager for the lifetime of the application). The python documentation has more examples on how to do complex things with urllib2 that you might also find useful.
Use this:
I think it's much simpler than using
urllib
. I don't understand why people love usingurllib
so much.Setting an environment var named http_proxy like this: http://username:password@proxy_url:port
Here is the method use urllib
Or if you want to install it, so that it is always used with urllib2.urlopen (so you don't need to keep a reference to the opener around):