python 获取以python 脚本提交的数据&以 curl 方式提交的 json / form 数据

一、以 curl 方式 发送请求

1、json格式数据

curl -H "Content-Type:Content-Type:application/json" -X POST -d '{"original_ip": "10.20.181.211", "host_name":["10.0.0.53"]}' http://ip:port/apis/getrestoreresult/

 python解析发送的请求

def getrestoreresult(request): 
    if request.method == 'POST':
        print(request.method)
        ipaddr = json.loads(request.body.decode("utf8")).get("original_ip")    #获取以json的格式提交数据
        hostiplist = json.loads(request.body.decode("utf8")).get("host_name")
        print("ipaddr : ",ipaddr)
        print("hostlist : ", hostiplist)

结果:

 

2、form数据:

curl -H "Content-Type:application/x-www-form-urlencoded" -X POST -d "original_ip=10.20.181.211&host_name=1.2.3.4&host_name=10.0.0.53" http://ip:port/apis/getrestoreresult/

host_name=1.2.3.4&host_name=10.0.0.53   #相当于host_name=['1.2.3.4','10.0.0.53']

python解析form数据:

def getneedRestoreIP(request):
    if request.method == 'POST':
        print(request.method)
        ipaddr = request.POST.get('original_ip')
        hostiplist = request.POST.getlist('host_name')

二、以Python方式

 Python方式发送request请求

# -*- coding: utf-8 -*-
import requests
import json

url="http://ip:port/apis/getrestoreresult/"
data = {
    'original_ip': '10.20.181.211',
    'original_port': '3306',
    'host_name': [
        '10.0.0.53', 
        '10.24.149.73',
        '1.12.13.12'
    ]
}
headers = {'Content-type': 'application/json'}
response = requests.post(url,data=json.dumps(data),headers=headers)
print(response.text)

 Python解析请求

def getneedRestoreIP(request):
    if request.method == 'POST':
        print(request.method)
        ipaddr = request.POST.get('original_ip')
        port = request.POST.get('original_port')
        hostiplist = request.POST.getlist('host_name')
发布了70 篇原创文章 · 获赞 11 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/YMY_mine/article/details/103506362