Mechanize (Python) is failing with 401 for me to open http digest URLs. I googled and tried debugging but no success.
My code looks like this.
import mechanize
project = "test"
baseurl = "http://trac.somewhere.net"
loginurl = "%s/%s/login" % (baseurl, project)
b = mechanize.Browser()
b.add_password(baseurl, "user", "secret", "some Realm")
b.open(loginurl)
mechanize doesn't use real browsers - it is a tool for programmatic web-browsing.
The mechanize module in Python is similar to perl WWW:Mechanize. It gives you a browser like object to interact with web pages. Here is an example on how to use it in a program.
Mechanize claims that the parameters should be uri, username and password as parameters, but you have four parameters. Four parameters are correct for urllib2.add_password, but then the first parameter should be the realm, not the uri.
http://wwwsearch.sourceforge.net/mechanize/
I'd try to change that first.
Does trac require digest? if not a next step could be to try using basic auth, as a test to see if that works, since you can add that with just addHeader:
import base64
from mechanize import Browser
browser = Browser()
browser.addheaders.append(('Authorization', 'Basic %s' % base64.encodestring('%s:%s' % (user, pwd))))
For http authentication with mechanize you need to provide the complete url to the add_password
method and not just the host base address.
import mechanize
project = "test"
baseurl = "http://trac.somewhere.net"
loginurl = "%s/%s/login" % (baseurl, project)
b = mechanize.Browser()
b.add_password(loginurl, "user", "secret", "some Realm")
b.open(loginurl)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With