Skip to content

Instantly share code, notes, and snippets.

@eikes
Created December 20, 2010 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eikes/748390 to your computer and use it in GitHub Desktop.
Save eikes/748390 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import urllib, urllib2
from BeautifulSoup import BeautifulSoup
def __main__():
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)
url = "http://rails-app.example.com/" # this is the url of the rails app
request = opener.open(url + "login") # login page
soup = BeautifulSoup(request.read())
request.close()
form = soup.find(id='new_user_session') #this is the id of the login form
auth_hash = form.find(attrs={'name':'authenticity_token'})['value']
action_url = form['action']
user_name = "my_user_name"
password = "my_password"
data = {'user_session[username]': user_name, 'user_session[password]': password, 'authenticity_token':auth_hash}
# Login:
request = opener.open(action_url, urllib.urlencode(data))
request.read()
request.close()
# Private Page:
request = opener.open(url + "/messages") # private page
soup = BeautifulSoup(request.read())
request.close()
print soup.find(id="messages") # do something with your private data...
__main__()
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment