Thursday, November 24, 2011

python web login script


this script assumes the login website first return a page with session ID, second page would present the login and password field associated with the session ID.


on redhat, make sure you have python installed and also install mechanize:
yum install python-mechanize.noarch


#!/usr/bin/python
import mechanize
from urllib2 import urlopen
from ClientForm import ParseResponse


res1 = mechanize.urlopen("http://login.domain.com/")
#print res1.read()
forms = ParseResponse(res1, backwards_compat=False)
form1 = forms[0]


print "sessionID "
print form1["sessionID"]


res2 = mechanize.urlopen(form1.click())
#print res2.read()
forms = ParseResponse(res2, backwards_compat=False)
form2 = forms[0]


print form2


form2["username"] = "myusername"
form2["password"] = "mypassword"
urlopen(form2.click())
print urlopen(form2.click()).read()

No comments: