zoukankan      html  css  js  c++  java
  • 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:Python27python.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:Python27python.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:Python27liburllib2.py", line 154, in urlopen
        return opener.open(url, data, timeout)
      File "C:Python27liburllib2.py", line 437, in open
        response = meth(req, response)
      File "C:Python27liburllib2.py", line 550, in http_response
        'http', request, response, code, msg, hdrs)
      File "C:Python27liburllib2.py", line 475, in error
        return self._call_chain(*args)
      File "C:Python27liburllib2.py", line 409, in _call_chain
        result = func(*args)
      File "C:Python27liburllib2.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

  • 相关阅读:
    栈和队列
    数组的遍历查找
    字符串的子串
    两个字符串
    字符串的遍历
    字符串的替换
    数组和矩阵
    Django 自带的ORM增删改查
    what's the CRSF ??
    Rabbitmq -Publish_Subscribe模式- python编码实现
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349410.html
Copyright © 2011-2022 走看看