import cookielib import getpass import urllib2 password = getpass.getpass() user = getpass.getuser() realm = None # This is the central authentication processing server # This is usually SSL protected and the password # should only be sent here authhost = 'https://centralserver.example.com' # This is the request URL # It is usually unprotected by SSL and the password # should never be sent here url = 'http://originserver.example.com/path' # handle the authentication and cookies cookiehand = urllib2.HTTPCookieProcessor() password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(user=user, passwd=password, uri=authhost, realm=realm) auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr) opener = urllib2.build_opener(auth_handler, cookiehand) urllib2.install_opener(opener) # make the request req = urllib2.Request(url=url) try: f = urllib2.urlopen(req) txt = f.read() f.close() except urllib2.HTTPError, e: print e print e.geturl() print e.headers txt = '' print txt