简体   繁体   English

403禁止,CSRF验证失败。 请求中止。 python请求

[英]403 forbidden and CSRF verification failed. Request aborted. python requests

import requests
import json
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36'}
post_data={"q":"","filters":{"sizes":["Large","MNE"],"sectors":[18],"countries":[228],"regions":["Northern America"],"years":[2015],"types":[]},"page":1}


with requests.Session() as s:
    for_cookies=s.get('http://database.globalreporting.org/search')
    # print(for_cookies.content)
    p = s.post('http://database.globalreporting.org/search/ajax/',data=json.dumps(post_data), headers=headers)
    print(p.content)

My chrome can visit the website but my code cannot. 我的浏览器可以访问该网站,但我的代码不能访问。 How to make my code able to visit the website? 如何使我的代码能够访问该网站? 在此处输入图片说明

I have included the csrf token and tried to call it. 我已包含csrf令牌并尝试调用它。 But I think the Django website must have used, 但是我认为Django网站一定使用过

if not request.is_ajax():
    return HttpResponse('Only ajax request')

Because I tried the code, 因为我尝试了代码,

import requests

with requests.Session() as client:
    for_cookies=client.get('http://database.globalreporting.org/search')
    csrf = client.cookies['csrftoken']
    print csrf
    post_data={"csrfmiddlewaretoken": csrf, "q":"","filters":{"sizes":["Large","MNE"],"sectors":[18],"countries":[228],"regions":["Northern America"],"years":[2015],"types":[]},"page":1}
    r = client.post('http://database.globalreporting.org/search/ajax/', data=post_data, headers=dict(Referer='http://database.globalreporting.org/search'))
    print r.text

The response I get is 我得到的答复是

YrZa9IIoFJZyXqeRXZnZ57s3vaoCUCul
Only ajax request

In general you have to use csrf token in these cases. 通常,在这些情况下,您必须使用csrf令牌。 But we can configure whether to use ajax only. 但是我们可以配置是否仅使用ajax。

Hope my answer helps you. 希望我的回答对您有所帮助。

You need to add CSRF token value to your headers: 您需要将CSRF令牌值添加到标头中:

with requests.Session() as s:
    for_cookies=s.get('http://database.globalreporting.org/search')

    headers =  
    {'X-CSRFToken': for_cookies.headers['Set-Cookie'].split('=')[1].split(';')[0],
    'Referer': 'http://database.globalreporting.org/search/',
    'X-Requested-With':'XMLHttpRequest'}

    p = s.post('http://database.globalreporting.org/search/ajax/',data=json.dumps(post_data), headers=headers)
    print(p.content)

Try this code and let me know in case of any issues 尝试此代码,如有任何问题,请通知我

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 禁止(403)CSRF验证失败。 请求中止。 Django的 - Forbidden (403) CSRF verification failed. Request aborted. Django 禁止(403)CSRF验证失败。 请求中止。 即使使用{%csrf_token%} - Forbidden (403) CSRF verification failed. Request aborted. Even using the {% csrf_token %} Django:“禁止 (403) CSRF 验证失败。请求中止。” 在 Docker 生产中 - Django: "Forbidden (403) CSRF verification failed. Request aborted." in Docker Production 如何解决“禁止(403)CSRF验证失败。 请求中止。” Django中的错误 - How to fix “Forbidden (403) CSRF verification failed. Request aborted.” error in django Forbidden (403) CSRF 验证失败。 请求中止。 登录页面不工作 - Forbidden (403) CSRF verification failed. Request aborted. login page not working CSRF 验证失败。 请求中止。 (Python 请求模块) - CSRF verification failed. Request aborted. (Python Request Module) CSRF验证失败。 请求中止。 Python 1.8 - CSRF verification failed. Request aborted. Python 1.8 禁止 (403) CSRF 验证失败。 请求中止。 失败的原因:来源检查失败与任何受信任的来源都不匹配 - Forbidden (403) CSRF verification failed. Request aborted. Reason given for failure: Origin checking failed does not match any trusted origins 禁止(403)CSRF验证失败。 请求使用django中止 - Forbidden (403) CSRF verification failed. Request aborted using django 禁止(403)CSRF验证失败。 请求中止。在Django中 - Forbidden (403) CSRF verification failed. Request aborted.in Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM