我正在尝试使用python'requests'lib提交一个https帖子。但是,我一直遇到此错误“ CSRF验证失败。请求中止”
我已经审查了论坛上报告的所有类似问题,但是找不到解决方案。
下面是我的python代码。感谢任何帮助。
import requests
payload = {
'targetip': 'www.ndtv.com'
}
header = {
'User-Agent': 'python-requests/2.20.1',
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*',
'Connection': 'keep-alive',
'Referer':'https://dnsdumpster.com/'
}
url = r'https://dnsdumpster.com/'
with requests.session() as s:
res = s.get(url)
cookie = s.cookies
res = requests.post(url, data=payload, headers = header, cookies = cookie)
print(res.text)
答案 0 :(得分:0)
缺少请求正文csrfmiddlewaretoken
,您可以将其保存在Cookie中
with requests.session() as s:
res = s.get(url)
payload['csrfmiddlewaretoken'] = s.cookies['csrftoken']
res = requests.post(url, data=payload, headers = header, cookies = s.cookies)
print(res.text)