3

How do I check whether the submit has been successful or not. When I give correct username it does not show anything. But even if I submit wrong username, it does not throw any errors. How to catch the errors or check success results. Any help will be greatly appreciated. Thank you.

code:

try:
    import mechanize
except ImportError:
    print "Could not import Mechanize. Please install from Mechanize website."
br = mechanize.Browser()
interletter = 'http://somesite.com/adminpanel/index.php'
br.open(interletter)
print br.geturl()

br.select_form(name="frm_login")
br["admin_userid"] = "user"
br["admin_password"] = "pword"
result2 = br.submit()


http://somesite.com/adminpanel/index.php
<response_seek_wrapper at 0x25a3cb0 whose wrapped object = <closeable_response at 0x25ab738 whose fp = <socket._fileobject object at 0x025A0970>>>
Robin
  • 5,366
  • 17
  • 57
  • 87
  • Have you looked in `result2` to see what response you're getting? – jonrsharpe Jan 29 '14 at 13:24
  • its giving me an object. I have updated the answer. – Robin Jan 29 '14 at 13:27
  • possible duplicate of [Python Mechanize keeps giving me 'response\_seek\_wrapper' when I try to use .open](http://stackoverflow.com/questions/3955301/python-mechanize-keeps-giving-me-response-seek-wrapper-when-i-try-to-use-open) – jonrsharpe Jan 29 '14 at 13:30
  • 2
    I am sorry but I don't think my question a duplicate of the above question. What I need is to check the status, i.e. logged in or not. I don't think it suffice my question though. – Robin Jan 29 '14 at 13:38

1 Answers1

1

It's possible that the web server you're dealing with is still sending an HTTP 200 status code, even if your login fails. They may just be including error text in the body of the response.

You should be able to call result2.read() to get the contents of the response object. Then, you'll have to examine or parse that to determine whether your login attempt was successful or not.

bjmc
  • 2,970
  • 2
  • 32
  • 46
  • Thank you for the answer. I tried `result2.read()` but its not display anything but this - `''`. What's am I doing wrong? – Robin Jan 29 '14 at 17:53
  • That's just an empty string. Maybe the server isn't returning anything? What HTTP status is it returning? Check `result2.code` – bjmc Jan 29 '14 at 18:39
  • If the server is returning 200 with an empty response body whether it fails or succeeds, then you'll have to determine in some other way. Can you look at the response headers? (`result2.info()` IIRC) Or else maybe you can inspect the cookie jar to see if it's set a session cookie for a successful login. – bjmc Jan 29 '14 at 19:57
  • `result2.info()` is returning this: `` – Robin Jan 29 '14 at 20:03
  • Oh, try `result2.info().headers` – bjmc Jan 29 '14 at 20:24
  • Its returning: `['Date: Thu, 30 Jan 2014 02:41:14 GMT\r\n', 'Server: Apache/2.2.24 (Unix) mod_ssl/2.2.24 OpenSSL/1.0.0-fips mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635\r\n', 'X-Powered-By: PHP/5.3.26\r\n', 'Expires: Thu, 19 Nov 1981 08:52:00 GMT\r\n', 'Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0\r\n', 'Pragma: no-cache\r\n', 'Connection: close\r\n', 'Transfer-Encoding: chunked\r\n', 'Content-Type: text/html\r\n']`. – Robin Jan 30 '14 at 02:44
  • There doesn't seem to be much useful there. Obviously *something* different happens on a successful login than a failed login, so you just need to determine what that is. Maybe it sets a cookie? Could you just make a second request that will succeed if you're logged in or failed if you aren't? – bjmc Jan 30 '14 at 17:28
  • What happens when you login on the browser? It just shows a blank page, every time? I don't believe it – User Feb 14 '14 at 01:58