开发者

Form-based authentication with Python

I'm trying to use a code read in Kent's Korner for Form-based authentication. At least I'm told the we开发者_JAVA百科b site I'm trying to read is form-based authenticated.

But I don't seem to be able to get past the login page. The code I'm using is

Import urllib, urllib2, cookielib, string

# configure an opener that will handle cookies
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)

# use the opener to POST to the login form and the protected page
params = urllib.urlencode(dict(username='user', password='stuff'))
f = opener.open('http://www.hammernutrition.com/forums/memberlist.php?mode=viewprofile&u=1323', params)
data = f.read()
f.close()
f = opener.open('http://www.hammernutrition.com/forums/memberlist.php?mode=viewprofile&u=1323')
data = f.read()
f.close()


You can simulate a web browser in Python without using much too resources with mechanize (Debian/Ubuntu package is called python-mechanize). It handles both cookies and submitting forms, just the way a web browser would do, one great example is a Python Dropbox Uploader script, which you can transform to your needs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜