V
V
Vladimir2019-04-01 12:23:06
Python
Vladimir, 2019-04-01 12:23:06

How to log in to a site with CSRF protection?

there is a need to parse the site, all information on it is available only after authorization, there are problems with authorization.
the site is most likely on django, when sending a post request to the server, the login, password, "remember me" flag and csrf tokens (one in the cookie, the other from the page body) go away, the problem is that all my attempts return 500
code example:

import requests,re

session = requests.Session()
r1 = requests.get('https://report.xxx.ru/user/security/login')
csrftoken = re.findall(r"[^\"\>\{\}\\]{86}==", r1.text) #вычленяем csrftoken из тела страницы
print(r1.cookies)

data={'login-form[username]': 'login', 'login-form[password]': 'password', 'login-form[rememberMe]': '0', '_csrf': csrftoken[1]}

r2 = session.post("https://report.xxx.ru/user/security/login", params=data)
print(data)
print(r2.status_code)
print(r2.text)

r = session.get('https://report.xxx.ru/report/report/view?id=1')

Can anyone tell me what this problem is connected with, or how to diagnose it?
upd: the desired result is a 302 code on successful authorization

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2019-04-17
Gavr @GhostGavr

the problem was that it was necessary to pass the parameters of the authorization form and csrf tokens using data and not params

r2 = session.post("https://report.xxx.ru/user/security/login", data=data)

E
Emil Revencu, 2019-04-11
@Revencu

Add allow_redirects=True to requests
You can also add verify=False

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question