简体   繁体   中英

Use mechanize to access a HTTP Basic authenticated webpage

In my current program, i'm accessing a HTTP Basic authenticated page like this, which works perfect:

import urllib2
url = 'http://test.localdomain/test.pl'
realm = 'Test DB'
username = 'foo'
password = 'bar'
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm, uri , username, password)
opener = urllib2.build_opener(auth_handler)
data = opener.open(url).read()

Now i want to click a button on that page after logging in. I found the mechanize library for Python which can do things like that easily. Unfortunately, i couldn't successfully do the same basic authentication as above when using mechanize. This is what i tried:

from mechanize import Browser
url = 'http://test.localdomain/test.pl'
realm = 'Test DB'
username = 'foo'
password = 'bar'
browser = Browser()
browser.add_password(url, username, password, realm)
browser.open(url)

But then i get the following exception:

HTTP Error refresh: The HTTP server returned a redirect error that would lead to an     
infinite loop.
The last 30x error message was:
OK

How can i fix this? Or can i let mechanize use the already working authhander created by urllib2 in my first snippet?

My script was throwing the same error. Here's how I fixed it.

browser.add_password(url, username, password, realm)
urllib2.urlopen(url)

You may need to use browser.click()to submit your form if there is one and/or create a "results site" variable and open that with urllib2.

The error can be ignored. When catching it, the program can regularly continue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM