Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

webforms - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...