how to get x csrf token in python

Solutions on MaxInterview for how to get x csrf token in python by the best coders in the world

showing results for - "how to get x csrf token in python"
Jamison
04 Feb 2018
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))