1import sys
2import requests
3URL = 'https://portal.bitcasa.com/login'
4client = requests.session()
5
6# Retrieve the CSRF token first
7client.get(URL) # sets cookie
8if 'csrftoken' in client.cookies:
9 # Django 1.6 and up
10 csrftoken = client.cookies['csrftoken']
11else:
12 # older versions
13 csrftoken = client.cookies['csrf']
14
15# Pass CSRF token both in login parameters (csrfmiddlewaretoken)
16# and in the session cookies (csrf in client.cookies)
17login_data = dict(username=EMAIL, password=PASSWORD, csrfmiddlewaretoken=csrftoken, next='/')
18r = client.post(URL, data=login_data, headers=dict(Referer=URL))