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.