django 只允许POST或者GET

允许的HTTP 方法:

@require_http_methods(["POST"])
def my_view(request):
    # ...
    return HttpResponse(status=201)

只允许 POST

# !/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
s=requests.session()
print s.headers
url="http://192.168.137.3:9000/articles/date/"

payload={}

r = requests.post(url, data=payload)

print r.status_code
# print r.content
print r.request


C:\Python27\python.exe D:/mysite/blog/a2.py
{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.18.4'}
201
<PreparedRequest [POST]>

Process finished with exit code 0






模拟GET请求:

import json
import urllib
import urllib2

url = "http://192.168.137.3:9000/articles/date/"
req = urllib2.Request(url)
print req
print type(req)

res_data = urllib2.urlopen(req)
res = res_data.read()
print res
print type(res)


C:\Python27\python.exe D:/mysite/blog/a3.py
<urllib2.Request instance at 0x02661648>
<type 'instance'>
Traceback (most recent call last):
  File "D:/mysite/blog/a3.py", line 10, in <module>
    res_data = urllib2.urlopen(req)
  File "C:\Python27\lib\urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 437, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 475, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 558, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 405: Method Not Allowed

Process finished with exit code 1

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页