zoukankan      html  css  js  c++  java
  • Django的request.POST没有获取到内容

      发送POST请求到服务器,但是在后端一直没有获取到数据

      客户端程序:

    import http.client
    import json
    import time
    
    #通过httplib,可以向某个url发送请求
    def RequestUrl(host,port,source,params,timeout):
            headers = {"Content-type": "application/json"}
            try:
                    conn = http.client.HTTPConnection(host,port,timeout)   #建立连接
                    conn.request('POST',source,params,headers)   #使用指定的method方法和url链接向服务器发送请求,params是要发送的部分,headers是HTTP头部
                    response = conn.getresponse()  #在请求发送后调用得到服务器返回的内容,返回的是一个HTTPResponse实例
                    original = response.read()   ##读取和返回response的body部分
                    print(original)
            except Exception as e:
                    raise e
            return original
    
    server_info = {
    "raid_adaptor_count":1,
    "nic_count":6,
    "cpumodel":"Intel",
    "cpu_core_count":"40",
    "asset_id":"1",
    "cpu_count":"2",
    }
    
    if      __name__ == "__main__":
    
            while True:
                    RequestData = urllib.parse.urlencode({'data':server_info})
                    result = RequestUrl('127.0.0.1','8000','/receive_server_info/',RequestData,30)
                    print('result: %s' % result)
                    time.sleep(3)
    

      在服务器端

    def receive_server_info(request):
            print(request.POST)
            return HttpResponse('successful')
    

      执行程序一直都不能获取到数据

      [29/Dec/2017 09:01:34] "POST /receive_server_info/ HTTP/1.1" 200 10
      <QueryDict: {}>

      最后通过在网上查阅是django没有对'Content-Type':'application/json'做处理,所以修改header为

      headers = {"Content-type": "application/x-www-form-urlencoded"}就可以了

      参考:http://blog.csdn.net/liuxingen/article/details/54176205

  • 相关阅读:
    MySQL主从复制
    高可用系列之Nginx
    02.PHP7.x编译详解
    01.PHP5.x编译详解
    月薪2500到年薪20+我经历了些什么?
    更换gitlab公网IP,引发的故障。
    博客资料汇总
    Nginx编译参数
    Zabbix3.0部署最佳实践
    SharePoint 2013让页面显示错误
  • 原文地址:https://www.cnblogs.com/homle/p/8476205.html
Copyright © 2011-2022 走看看