Skip to content

Instantly share code, notes, and snippets.

@rishabhnambiar
Forked from npalko/aspx.py
Created June 22, 2017 11:54
Show Gist options
  • Save rishabhnambiar/0be0411e824c9b295d23ba2e567de006 to your computer and use it in GitHub Desktop.
Save rishabhnambiar/0be0411e824c9b295d23ba2e567de006 to your computer and use it in GitHub Desktop.
Authenticate with ASP using Python
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