Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd recommend considering using the very excellent <code>requests</code> library which provides a nice abstraction to make urllib2 a bit easier to use.</p> <p>With <code>requests</code> you can simply do:</p> <pre><code>r = requests.get('https://api.github.com', auth=('user', 'pass')) </code></pre> <p>It supports all of the request methods needed to make REST calls as well (POST, PUT, DELETE, etc...).</p> <p>You can find more here:</p> <p><a href="http://pypi.python.org/pypi/requests" rel="nofollow">http://pypi.python.org/pypi/requests</a></p> <hr> <p>If you absolutely <strong>MUST</strong> use plain old urllib2, here is an example of how it can be done:</p> <pre><code>import urllib2 theurl = 'http://www.someserver.com/toplevelurl/somepage.htm' username = 'johnny' password = 'XXXXXX' passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, theurl, username, password) # because we have put None at the start it will always # use this username/password combination for urls # for which `theurl` is a super-url authhandler = urllib2.HTTPBasicAuthHandler(passman) opener = urllib2.build_opener(authhandler) urllib2.install_opener(opener) # All calls to urllib2.urlopen will now use our handler # Make sure not to include the protocol in with the URL, or # HTTPPasswordMgrWithDefaultRealm will be very confused. # You must (of course) use it when fetching the page though. pagehandle = urllib2.urlopen(theurl) # authentication is now handled automatically for us </code></pre> <p>More can be found here: <a href="http://www.voidspace.org.uk/python/articles/authentication.shtml" rel="nofollow">http://www.voidspace.org.uk/python/articles/authentication.shtml</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload