开发者

Mechanize not logging in to site properly

After spending the best pa开发者_开发问答rt of 3 hours getting nowhere, i thought i would ask a question myself. i am using python and mechanize's Browser. I am trying to login to my home router. i can get to the login page, fill the password field (eg br.form['password'] = 'mypassword' etc), but now i am stuck because no matter what i try, i always get sent to a page saying i am forbidden/session has timed out i am handling cookies with a cookie jar, i have handled robots, but it still doesnt love me enough to let me login.... help? if you have any suggestions as to why i can not login successfully, please, please, please share xxx

EDIT:

it does have javascript, but i am simulating it

**EDIT2:**

i just realized that the javascript i am simulating is for pressing the disconnect button on the previous page...... probably fix my problem if i emulate the javascript for this page ......

i am faking the user agent

i will have a look w/ wireshark xxx

ps thanks for replying so fast xx

UPDATE: sigh. i think it may just be one of those things where you just have to say f it and move on because i am emulating the javascript it needed exactly as it is done (just checking, MD5 == hashlib.md5("...").hexdigest()?).... thanks anyway people xxx


Since you have to deal with Javascript, I'd try to use WebDriver. It has Python bindings and it's not that hard to use.


Here's some sample code for mechanize implementing cookies and a fake user-agent -- might help out...

import mechanize

cookies = mechanize.CookieJar()
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookies))
opener.addheaders = [("User-agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6; en-us) AppleWebKit/533.4 (KHTML, like Gecko) Version/4.1 Safari/533.4"),]
mechanize.install_opener(opener)

url = "http://your_home_router_address"
content = mechanize.urlopen(url)
return content.read()


Are you sure there is no Javascript in the page, manipulating cookies or other headers ?

I had those kind of problems with some (certainly not Web 2.0 :-) web sites, and had to analyze and emulate those scripts somehow... :-q

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜