""" Situation: fetch some data from a web form that requires authentication. Code used (straight from the example): password_mgr = urllib2.HTTPPasswordMgr() user = "" pw = "" password_mgr.add_password( "CERN NICE authentication", "ab-dep-op-elogbook.web.cern.ch", user, pw) passwordhandler = urllib2.HTTPBasicAuthHandler(password_mgr) cookiehandler = urllib2.HTTPCookieProcessor() handlers = [passwordhandler, cookiehandler] opener = urllib2.build_opener(*handlers) opener.open(baseURL+"/login.php") This code works perfectly, as long as user name and password are correct. Hoever, if the password is wrong, the behaviour of the program (for this particular web site, at least) is so that it repeats the request forever. The result is that the web site in question blocks the account! Suggested fix below: use HTTPOncePasswordMgr instead of HTTPPasswordMgr """ class HTTPOncePasswordMgr(urllib2.HTTPPasswordMgr): """Password manager that submits each passwor only once.""" def __init__(self): self.visited = set() urllib2.HTTPPasswordMgr.__init__(self) def find_user_password(self, realm, authuri): if (realm, authuri) in self.visited: print "Authentication failed: I guess you password is wrong." return None, None self.visited.add((realm, authuri)) return urllib2.HTTPPasswordMgr.find_user_password(self, realm, authuri)