Skip to content

Instantly share code, notes, and snippets.

@axiak
Created October 26, 2011 21:55
Show Gist options
  • Save axiak/1318009 to your computer and use it in GitHub Desktop.
Save axiak/1318009 to your computer and use it in GitHub Desktop.
# How it would work in strict python/ Java's HTTPClient
import urllib2
import cookielib
import urllib
import re
csrf_token_re = re.compile(r"csrfmiddlewaretoken.*?value=['\"?](\w+)")
def main():
host = 'splashchicago.learningu.org'
cookiejar = cookielib.CookieJar()
url_opener = urllib2.build_opener()
url_opener.add_handler(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
main_request = urllib2.Request('https://%s/' % host)
main_response = url_opener.open(main_request)
match = csrf_token_re.search(main_response.read())
if match:
token = match.group(1)
else:
raise RuntimeError("Could not get CSRF token.")
login_request = urllib2.Request('https://%s/myesp/ajax_login/' % host,
urllib.urlencode({
'username': 'axiak',
'password': '***password***',
'csrfmiddlewaretoken': token,
}))
response = url_opener.open(login_request)
# Do stuff with url_opener to open the ajax scheduling pages...
#################################################
# Using python's mechanize library
#################################################
import mechanize
def main():
host = 'splashchicago.learningu.org'
br = mechanize.Browser()
br.open('https://%s/' % host)
br.select_form(name=br.forms().next().name)
br['username'] = 'axiak'
br['password'] = '***password***'
response = br.submit()
# Do stuff with br to open the ajax scheduling pages...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment