开发者

Python mechanize login to website

I'm trying to log into a website using Python and Mechanize, however, I'm running into trouble when trying to get the POST data to behave as I want.

Essentially I want to replicate this using mechanize and Python:

wget --quiet --save-cookies cookiejar --keep-session-cookies --post-data "action=login&login_nick=USERNAME&login_pwd=PASSWORD" -O outfile.htm http://domain.com/index.php

The form looks like this:

<login POST http://domain.com/index.php application/x-www-form-urlencoded
  <TextControl(login_nick=USERNAME)>
  <PasswordControl(login_pwd=PASSWORD)>
  <CheckboxControl(login_auto=[1])>
  <SubmitButtonControl(<None>=) (readonly)>>

Setting the appropriate values and submitting the form isn't a problem, but that leaves out the "action=login"-part.

response = self.browser.open(self.url+"/index.php")
self.browser.select_form(name="login")

self.browser["login_nick"] = self.encoded_username
self.browser["login_pwd"] = self.encoded_password

self.browser.method = "POST"

response = self.browser.open(self.browser.submit())

print (response.read())

Now the question is, how do I add the action=login part?

Edit: Okay, so I added a hidden field named action and set the value to login. Analyzing the TCP stream with Wireshark, the POST data is indeed structured the way it should. However, it seems that mechanize is messing with my urlencoding (I have already urlencoded the values specifically for the charset that the website uses). For example, my username contains an Å - which I have urlencoded to %C5. However, when 开发者_如何学编程it's sent with mechanize, it's displayed as %25C5. How do I stop mechanize from changing the strings?

EDIT: I realized that rather than fighting mechanize, I could just not urlencode my strings before sending them. Case closed.


Mechanize seems to urlencode the strings anyway, so there's no point in fighting it. This is the final solution (obviously not syntactically valid, but hopefully you get the idea).

import mechanize

self.browser = mechanize.Browser()
self.browser.open(self.url)
self.browser.select_form(name="login")

self.browser["login_nick"] = self.username
self.browser["login_pwd"] = self.password
self.browser.new_control("HIDDEN", "action", {})
control = self.browser.form.find_control("action")
control.readonly = False
self.browser["action"] = "login"
self.browser.method = "POST"
self.browser.action = self.url

response = self.browser.submit()
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜