requests模块发送数据

 

通过json dumps发送

import requests
import json

def agent():
    """
    执行命令采集硬件信息
    将执行的信息发送给API
    :return:
    """
    info = {'cpu':{'xx':'XXX'},'disk':'xxx'}
    ret = requests.post(
        url='http://127.0.0.1:8000/app01/asset/',
        # data=info
        # json=info
        data=json.dumps(info).encode('gbk')
    )
    print(ret)

agent()

 

服务器通过json load接收

from django.shortcuts import render,HttpResponse
from django.views.decorators.csrf import csrf_exempt
import json

@csrf_exempt
def asset(request):
    """
    接收数据
    保存数据到数据库
    :param request:
    :return:
    """
    print(request.POST)
    print(request.body)
    print(json.loads(request.body.decode('gbk')))
    return HttpResponse('ok')

 

posted @ 2019-08-28 10:40  ivy_wang  阅读(269)  评论(0编辑  收藏  举报