<p>What's the best way to specify a proxy with username and password for an http connection in python?</p>

[{'answer_id': 34116, 'author': 'bernhardrusch', 'author_id': 3056, 'author_profile': 'https://Stackoverflow.com/users/3056', 'pm_score': 6, 'selected': False, 'text': "<p>This works for me:  </p>\n\n<pre><code>import urllib2\n\nproxy = urllib2.ProxyHandler({'http': 'http://\nusername:password@proxyurl:proxyport'})\nauth = urllib2.HTTPBasicAuthHandler()\nopener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)\nurllib2.install_opener(opener)\n\nconn = urllib2.urlopen('http://python.org')\nreturn_str = conn.read()\n</code></pre>\n"}, {'answer_id': 35443, 'author': 'gz.', 'author_id': 3665, 'author_profile': 'https://Stackoverflow.com/users/3665', 'pm_score': 3, 'selected': False, 'text': '<p>The best way of going through a proxy that requires authentication is using <a href="http://docs.python.org/lib/module-urllib2.html" rel="noreferrer">urllib2</a> 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).</p>\n\n<pre><code>import urllib2\n\ndef get_proxy_opener(proxyurl, proxyuser, proxypass, proxyscheme="http"):\n    password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()\n    password_mgr.add_password(None, proxyurl, proxyuser, proxypass)\n\n    proxy_handler = urllib2.ProxyHandler({proxyscheme: proxyurl})\n    proxy_auth_handler = urllib2.ProxyBasicAuthHandler(password_mgr)\n\n    return urllib2.build_opener(proxy_handler, proxy_auth_handler)\n\nif __name__ == "__main__":\n    import sys\n    if len(sys.argv) &gt; 4:\n        url_opener = get_proxy_opener(*sys.argv[1:4])\n        for url in sys.argv[4:]:\n            print url_opener.open(url).headers\n    else:\n        print "Usage:", sys.argv[0], "proxy user pass fetchurls..."\n</code></pre>\n\n<p>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 <a href="http://docs.python.org/lib/urllib2-examples.html" rel="noreferrer">more examples on how to do complex things with urllib2</a> that you might also find useful.</p>\n'}, {'answer_id': 142324, 'author': 'Tony Meyer', 'author_id': 4966, 'author_profile': 'https://Stackoverflow.com/users/4966', 'pm_score': 2, 'selected': False, 'text': '<p>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):</p>\n\n<pre><code>import urllib2\nurl = \'www.proxyurl.com\'\nusername = \'user\'\npassword = \'pass\'\npassword_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()\n# None, with the "WithDefaultRealm" password manager means\n# that the user/pass will be used for any realm (where\n# there isn\'t a more specific match).\npassword_mgr.add_password(None, url, username, password)\nauth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)\nopener = urllib2.build_opener(auth_handler)\nurllib2.install_opener(opener)\nprint urllib2.urlopen("http://www.example.com/folder/page.html").read()\n</code></pre>\n'}, {'answer_id': 3942980, 'author': 'ducu', 'author_id': 45712, 'author_profile': 'https://Stackoverflow.com/users/45712', 'pm_score': 4, 'selected': False, 'text': '<p>Setting an environment var named <strong>http_proxy</strong> like this: <strong>http://username:password@proxy_url:port</strong></p>\n'}, {'answer_id': 36216636, 'author': 'daz', 'author_id': 799873, 'author_profile': 'https://Stackoverflow.com/users/799873', 'pm_score': 2, 'selected': False, 'text': '<p>Here is the method use urllib </p>\n\n<pre><code>import urllib.request\n\n# set up authentication info\nauthinfo = urllib.request.HTTPBasicAuthHandler()\nproxy_support = urllib.request.ProxyHandler({"http" : "http://ahad-haam:3128"})\n\n# build a new opener that adds authentication and caching FTP handlers\nopener = urllib.request.build_opener(proxy_support, authinfo,\n                                     urllib.request.CacheFTPHandler)\n\n# install it\nurllib.request.install_opener(opener)\n\nf = urllib.request.urlopen(\'http://www.python.org/\')\n"""\n</code></pre>\n'}, {'answer_id': 41920303, 'author': 'Aminah Nuraini', 'author_id': 1564659, 'author_profile': 'https://Stackoverflow.com/users/1564659', 'pm_score': 6, 'selected': True, 'text': '<p>Use this:</p>\n\n<pre><code>import requests\n\nproxies = {"http":"http://username:password@proxy_ip:proxy_port"}\n\nr = requests.get("http://www.example.com/", proxies=proxies)\n\nprint(r.content)\n</code></pre>\n\n<p>I think it\'s much simpler than using <code>urllib</code>. I don\'t understand why people love using <code>urllib</code> so much.</p>\n'}]
['https://Stackoverflow.com/questions/34079', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3573/']