Skip to content

Instantly share code, notes, and snippets.

@yosemitebandit
Created February 12, 2012 02:46
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yosemitebandit/1805918 to your computer and use it in GitHub Desktop.
Save yosemitebandit/1805918 to your computer and use it in GitHub Desktop.
using the request lib's sessions to login; bonus: beautiful soup finds the csrf token
#!/usr/bin/env python
'''
testing a login to meduele using sessions
meduele checks csrf tokens with every request, even during login
'''
import requests
from BeautifulSoup import BeautifulSoup
# need to capture a valid csrf token
# first visit the login page to generate one
s = requests.session()
response = s.get('https://callmeduele.com/login')
# extract the token
soup = BeautifulSoup(response.text)
for n in soup('input'):
if n['name'] == '_csrf_token':
token = n['value']
break
# now post to that login page with some valid credentials and the token
auth = {
'userName': 'batman'
, 'password': 'j0kersuck5'
, '_csrf_token': token
}
s.post('https://callmeduele.com/login', data=auth)
# now we should be authenticated, try visiting a protected page
response = s.get('https://callmeduele.com/cases')
print response.text
@rohitsoftt
Copy link

rohitsoftt commented Oct 2, 2019

Hi Guys,
if you are using beautifulsoup4 4.3.2 then you should need to do some changes in the above code.
use from bs4 import BeautifulSoup instead of from BeautifulSoup import BeautifulSoup

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