Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to find csrf token python

// Source: https://localcoder.org/get-csrf-token-using-python-requests

//Solution 1: 
from bs4 import BeautifulSoup
soup = BeautifulSoup(response.text, 'lxml')
csrf_token = soup.select_one('meta[name="csrf-token"]')['content']

// Solution 2: 
import requests
from bs4 import BeautifulSoup
headers = {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 
           (KHTML, like Gecko) Chromium/80.0.3987.160 Chrome/80.0.3987.163 
           Safari/537.36'
 }
login_data = {
             'name' : 'USERNAME',
             'pass' : 'PASSWORD',
             'form_id':'new_login_form',
             'op':'login'
  }

with requests.Session() as s:
    url = 'https://www.codechef.com/'
    r = s.get(url,headers=headers,verify=False)
    #print(r.content) # to find name of csrftoken and form_build_id
    soup = BeautifulSoup(r.text, 'lxml')

    csrfToken = soup.find('input',attrs = {'name':'csrfToken'})['value']
    form_build_id = soup.find('input',attrs = {'name':'form_build_id'}) 
    ['value']

    login_data['csrfToken'] = csrfToken
    login_data['form_build_id'] = form_build_id

    r = s.post(url,data=login_data,headers = headers)
    print(r.content)
Comment

how to add csrf token in python requests

import requests

LOGIN_URL = 'https://examplenotarealpage.com'
headers = {
    'accept': 'text/html,application/xhtml+xml,application/xml',
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}

response = requests.get(LOGIN_URL, headers=headers, verify=False)

headers['cookie'] = '; '.join([x.name + '=' + x.value for x in response.cookies])
headers['content-type'] = 'application/x-www-form-urlencoded'
payload = {
    'username': 'user_name',
    'password': 'randompass123'
}

response = requests.post(LOGIN_URL, data=payload, headers=headers, verify=False)
headers['cookie'] = '; '.join([x.name + '=' + x.value for x in response.cookies])
Comment

PREVIOUS NEXT
Code Example
Python :: tkinter events 
Python :: python last element list 
Python :: remove spaces from input python 
Python :: python string contains substring 
Python :: python enumerate start at 1 
Python :: max of matrix numpy 
Python :: TabError: inconsistent use of tabs and spaces in indentation 
Python :: pandas concat / merge two dataframe within one dataframe 
Python :: pandas summarize all columns 
Python :: python 3.9.5 installed update default version 
Python :: seaborn heatmap parameters 
Python :: pandas replace zero with blank 
Python :: python random word 
Python :: remove character python 
Python :: check string equal with regular expression python 
Python :: get os information python 
Python :: binary string to hex python 
Python :: order dictionary by value python 
Python :: python fernet 
Python :: pandas read_csv nan as empty string 
Python :: python unit testing machine learning 
Python :: python append element to array 
Python :: from random import choice 
Python :: file handling modes in python 
Python :: remove all rows without a value pandas 
Python :: python make a list of odd numbers 
Python :: python matplotlib pyplot 
Python :: how to append data to csv file in python without replacing the already present text 
Python :: variable naming rule in python 
Python :: print % in python 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =