views:

1181

answers:

4

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)
+1  A: 

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:

browser = Browser()
browser.addHeader('Authorization', 'Basic %s:%s' % (user, pwd))
Lennart Regebro
The last parameter is optional: realm, defaulting to None.
Vinay Sajip
Ah, OK. Never seen that one, and docs doesn't mention it... But anyway, you catched the fact that he was only opening the login screen, so this is in fact not an authentication error at all.
Lennart Regebro
help -> add_password(self, url, user, password, realm=None) method of mechanize._mechanize.Browser instanceSecondly similar script using Twill works well. Twill in turn uses mechanize only.
Shekhar
A: 

Depending on how complex your web automation project is, consider using iMacros. Unlike Mechanize it runs in the web browser, so it works with most websites out of the box.

I use both the free Firefox addon via a command line (on our Linux server) and the iMacros Scripting Edition via COM object (on a Windows VMware for sites with Flash and Java).

Command line (freeware & open source): http://wiki.imacros.net/iMacros_for_Firefox#Command_Line_Support

COM object (paid version required): wiki.imacros.net/Python

Tim

Interesting, however by the requirement, I need something executed within existing python program.
Shekhar
A: 

#!/usr/bin/env python # -- coding: utf-8 --

import mechanize

a=mechanize.Browser()
a.open("http://www.facebook.com/login.php")
a.select_form(nr=0)  #form number.
a["email"]="mailaddress"
a["pass"]="password"
a.submit()
print a
Muslu