-
-
Save rishabhnambiar/0be0411e824c9b295d23ba2e567de006 to your computer and use it in GitHub Desktop.
Authenticate with ASP using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import BeautifulSoup | |
import cookielib | |
import urllib | |
import urllib2 | |
username,password = 'SomeUser','$ecret' | |
controller = 'http://example.com/SomeLameLoginPage.aspx' | |
post = { | |
'__EVENTVALIDATION' : None, # These "__" will be present for every *.aspx page | |
'__VIEWSTATE' : None, | |
'btnLogin': 'Login', # These fields are application dependent | |
'tbLogin' : username, | |
'tbPassword' : password | |
} | |
cookiejar = cookielib.CookieJar() | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar)) | |
urllib2.install_opener(opener) | |
# Snag ASP fields so we can properly POST our login information and get | |
# out authentication cookie | |
f = opener.open(controller) | |
soup = BeautifulSoup.BeautifulSoup(f) | |
for i in [k for k,v in post.iteritems() if v is None] | |
post[i] = soup.find('input', {'id':i})['value'] | |
opener.open(controller, urllib.urlencode(post)) | |
# Now that we're logged in and have our cookie, we can get some content | |
f = opener.open(contentController) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment