Flask CSRF-токен отсутствует между сервером; py и client.py - PullRequest
0 голосов
/ 14 января 2020

Для начала я уточняю, что Я не использую здесь никаких форм . Просто запросы между python скриптами.

У меня проблемы с реализацией csrf_token между файлом server.py и файлом client.py . Проблема в том, что я не могу получить повар ie с сервера в моем клиенте. Я настолько заблокирован, что даже если бы я мог получить его, я не уверен, что мне делать дальше

Это мой код для server.py

from flask import Flask, jsonify
from flask_wtf.csrf import CSRFProtect()

app = Flask(__name__)
app.config.update(SESSION_COOKIE_NAME='my-session', SECRET_KEY="my_secret_key", SESSION_COOKIE_SECURE=True)
csrf = CSRFProtect(app)
csrf.init_app(app)

@app.before_request
def check_csrf():
    csrf.protect()

@app.route('/json_format/', methods=['GET','POST'])
def json_format():
# ... ... ...

Это мой client.py

import os, sys , requests
from flask import Flask, jsonify, Session, request

URL = "http://127.0.0.1:5000/json_format/"

client = requests.Session() 
requete = client.get(URL) # should retrieve cookie, but is empty => <RequestsCookieJar[]> 

print(requete.headers) # {'Content-Type': 'text/html', 'Content-Length': '192', 'Server': 'Werkzeug/0.15.2 Python/3.7.3', 'Date': 'Tue, 14 Jan 2020 13:48:47 GMT'} 
print(client.headers) # {'User-Agent': 'python-requests/2.21.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
print(requete.cookies, client.cookies) # => they are empty <RequestsCookieJar[]> <RequestsCookieJar[]>

if 'SECRET_KEY' in requete.cookies:
    csrftoken = requete.cookies['SECRET_KEY']

payload = { "some texts..." } # => dict
payload['csrf_token'] = csrftoken # => I try to return the csrf_token in the payload and in the header -_- this is my nindô :)

headers = {'Content-type': 'application/json', 'Accept': 'text/plain', 'Referer': URL, 'csrf_token' : csrftoken}
req_serveur = requests.post(URL, data=json.dumps(payload ), headers=headers)
print(req_serveur.text) # => 400 Bad Request The CSRF token is missing

Буду признателен за любую помощь по этим вопросам, ребята!

Антуан

PS: пусть я знаю, если / как я могу улучшить свой пост:)

...