当我使用以下 Python 代码向我的 Django 网站发送 POST 请求时,出现 403: Forbidden 错误。
url = 'http://www.sub.example.com/'
values = { 'var': 'test' }
try:
data = urllib.urlencode(values, doseq=True)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
except:
the_page = sys.exc_info()
raise
当我打开任何其他网站时,它都可以正常工作。example.com
也是 Django 网站,并且也运行正常。
我想,这是 Django 配置问题,谁能告诉我我应该做什么来提供对我的脚本的访问?
看这里https://docs.djangoproject.com/en/dev/ref/csrf/#how-to-use-it https://docs.djangoproject.com/en/dev/ref/csrf/#how-to-use-it.
尝试用以下标记标记您的视图@csrf_exempt
。这样,Django 的 CSRF 中间件将忽略 CSRF 保护。您还需要使用from django.views.decorators.csrf import csrf_exempt
. See: https://docs.djangoproject.com/en/dev/ref/csrf/#utilities https://docs.djangoproject.com/en/dev/ref/csrf/#utilities
请注意,在您的视图中禁用 CSRF 保护就等于为 CSRF 攻击打开了大门。
如果安全对您来说至关重要,那么请考虑使用@csrf_exempt
其次是@requires_csrf_token
(see: https://docs.djangoproject.com/en/dev/ref/csrf/#unprotected-view-needs-the-csrf-token https://docs.djangoproject.com/en/dev/ref/csrf/#unprotected-view-needs-the-csrf-token)。然后,在您的脚本中传递此令牌即可。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)