Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to access http://forum.kriminala.net via Mechanize and parse my inbox messages.

From the html code, I can see that the login form is in the nested iframe of the main page:

<iframe src="login/" style="width: 100%; height: 124px; border-bottom: 2px solid #DDE5EA; box-shadow: 0px 0px 10px #ccc;" frameborder="0" vspace="0" scrolling="no" hspace="0">
...
<form action="" class="auth_form" method="post">
<input type="hidden" name="referer" value="http%3A%2F%2Fforum.kriminala.net%2F">
<input type="text" class="text_input" name="username" placeholder="Имя пользователя" value="" tabindex="1">
<input type="password" class="text_input" name="password" placeholder="Пароль" tabindex="2">
<input type="checkbox" id="autologin" checked="checked" name="autologin" tabindex="3">
<label for="autologin">Запомнить меня</label>
<input type="submit" class="submit_button" id="submit_button" name="login" value="" tabindex="3">
</form>
...
</iframe>

so I navigate to http://forum.kriminala.net/login, find the form there and submit it with my username and password, outputting the result in a file (to see if I got logged in successfully).

br=mechanize.Browser()
br.open("http://forum.kriminala.net/login/")
br.select_form(nr=0)
br["username"]="12n"
br["password"]="123456"
response=br.submit()
htmlpage=open("response.html","w")
htmlpage.writelines(response.get_data())
htmlpage.close()

However, all I see in the file is this:

<script type="text/javascript">
window.top.location = 'http://forum.kriminala.net/';
</script>

My next thought is maybe I should go to the main mage manually, so I open the main page in Mechanize, put it into an html file to open in a browser, but the file still looks like I am not logged in.

How do I deal with this?

P.S. I am a complete Python noob, so maybe I just don't know what to google to get my answers. If this is the case, please just point me in the right direction.

Thanks!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

All seems OK. The resulting page redirects you to the main page (or possibly to wherever your were before logging in) with JavaScript which is a reasonable thing to do. Since there's no JavaScript in your "browser", you'll need to navigate manually wherever you need to.

The actual result of the login should be a Set-Cookie: header in one of the responses. You need to use that cookie in subsequent request headers to make the server consider you logged in. See HTTP cookie @wikipedia for more theory.

Emulating a Browser in Python with mechanize appears to have relevant code to do that in mechanize, specifically the br.set_cookiejar() command.

share|improve this answer
    
yep, this works perfectly!, thanks a lot! –  kurtgn Sep 26 at 16:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.