Skip to content

Instantly share code, notes, and snippets.

@npalko
Created July 22, 2011 21:35
Show Gist options
  • Save npalko/1100480 to your computer and use it in GitHub Desktop.
Save npalko/1100480 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)
@ILoveLuci
Copy link

This is great. Thanks.

Any chance of a python3 compatible version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment