Python: Fetching HTTPS urls which require username/password authentication

0 comments

It wasn't the easiest solution to find on the net, but a fairly simple one to implement.

Some solutions even suggest recompiling Python with OpenSSL installed. Sorry, too much trouble, not an option!

from urllib2 import URLError, HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, install_opener, build_opener

# Set up a HTTPS request with username/password authentication
try:
# create a password manager
password_mgr = HTTPPasswordMgrWithDefaultRealm()

# Add the username and password.
password_mgr.add_password(None, feed_url, "your_username", "your_password")
opener = build_opener(HTTPBasicAuthHandler(password_mgr))
file = opener.open(feed_url)

# After you get the "file", you pretty much work with it the same way as you normally would.
xml = etree.parse(file)

except URLError, e:
print 'URLError: "%s"' % e
raise

Brush your hands off boys, we're done here.

Now to solve more pressing issues...

ExtRS
How to get that damn toilet tissue?

Source

0 comments:

Post a Comment

Leave your thoughts ...
---
If you are having trouble with copy/pasting in comments, you need to sign in or click 'Preview'. For more information about this Firefox bug, see here.

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog