def add_auth(realm, uri, user, passwd): """ >> add_auth <realm> <uri> <user> <passwd> Add HTTP Basic Authentication information for the given realm/uri. """ # swap around the type of HTTPPasswordMgr and # HTTPPasswordMgrWithDefaultRealm depending on if with_default_realm # is on or not. if _options['with_default_realm']: realm = None if browser.creds.__class__ == mechanize.HTTPPasswordMgr: passwds = browser.creds.passwd browser.creds = mechanize.HTTPPasswordMgrWithDefaultRealm() browser.creds.passwd = passwds print >> OUT, 'Changed to using HTTPPasswordMgrWithDefaultRealm' else: if browser.creds.__class__ == mechanize.HTTPPasswordMgrWithDefaultRealm: passwds = browser.creds.passwd browser.creds = mechanize.HTTPPasswordMgr() browser.creds.passwd = passwds print >> OUT, 'Changed to using HTTPPasswordMgr' browser.creds.add_password(realm, uri, user, passwd) print >> OUT, "Added auth info: realm '%s' / URI '%s' / user '%s'" % ( realm, uri, user, )
def __init__(self): # # create special link/forms parsing code to run tidy on HTML first. # factory = ConfigurableParsingFactory() # # Create the mechanize browser. # b = PatchedMechanizeBrowser(history=HistoryStack(), factory=factory) self._browser = b self.result = None self.last_submit_button = None # # create & set a cookie jar. # policy = mechanize.DefaultCookiePolicy(rfc2965=True) cj = mechanize.LWPCookieJar(policy=policy) self._browser.set_cookiejar(cj) self.cj = cj # Ask for MIME type 'text/html' by preference. self._browser.addheaders = [("Accept", "text/html; */*")] # ignore robots.txt self._browser.set_handle_robots(None) # create an HTTP auth handler self.creds = mechanize.HTTPPasswordMgr() # do handle HTTP-EQUIV properly. self._browser.set_handle_equiv(True) # callables to be called after each page load. self._post_load_hooks = []