from urllib2 import (HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, build_opener, install_opener, urlopen, HTTPError) password_mgr = HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, "http://alanstorm.com/testbed", 'admin','the-password') handler = HTTPBasicAuthHandler(password_mgr) opener = build_opener(handler) install_opener(opener) try: f = urlopen("http://alanstorm.com/testbed/password-test/test.txt") print f.read(); except HTTPError, e: print e.code print e.headers