沒有csrf標記我得到一個403響應,所以它看起來像我需要它。目前我正在試圖做到這一點:如何獲取Django服務器的python POST請求的csrf標記
import requests
import sys
URL = 'http://127.0.0.1:8000/toasterinfo'
client = requests.session()
# Retrieve the CSRF token first
client.get(URL) # sets cookie
csrftoken = client.cookies['csrf']
r = client.post(URL, data={'number': 12524,
'type': 'issue', 'action': 'show', "csrfmiddlewaretoken": csrftoken}, headers=dict(Referer=URL))
print(r)
但我得到一個錯誤:
Traceback (most recent call last):
File "django_client.py", line 10, in <module>
csrftoken = client.cookies['csrftoken']
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/cookies.py", line 327, in __getitem__
return self._find_no_duplicates(name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/cookies.py", line 398, in _find_no_duplicates
raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
KeyError: "name='csrftoken', domain=None, path=None"
我在做什麼錯?
是的...我希望能夠保持CSRF驗證... – doubleOK