Python-for循环迭代读取多个参数,发送POST请求

本来想用java来写批量添加设备到IOT平台的,但是,想尝试用Python脚本写一下,就请教一下朋友,使用for k,v in 来读取多个参数,实现用POST请求 批量添加的功能

 

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json
import time
import requests

url = 'http://127.0.0.1:8080/add/serialnumber'
headers = {'Accept': "application/json", 'Content-Type': 'application/json',
           'Authorization': 'Basic cxxxxXXXxxXXXxXXx'}
a = {'869405030990008':['宝山路001号','TEST','TEST-ABC-NB01'], '869405030980009': ['宝山路002号','TEST','TEST-ABC-NB02'], '86940503099000a': ['宝山路00a号','TEST','TEST-ABC-NB01']}
for key, value in a.items():
    data = {"serialNumber": key,  "protocol": "NB-IOT",  "groupName": "DM.TEST.TESTADD",
            "additionalParams": {'adaptationLayerName': 'TEST_NB', 'tag': 'TEST-ABC-NB-V1',"deployaddr": value[0], 'manufacturer':value[1], 'model':value[2],}}
    data = json.dumps(data)
    res = requests.post(url, data=data, headers=headers, auth=('test', 'Test#001'))
    print(key)
    print(res.content)
    time.sleep (1)

 

posted @ 2022-01-27 18:36  zhangdaopin  阅读(300)  评论(0编辑  收藏  举报