#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/2/24 23:01 # @Author : LiShan # @Email : [email protected] # @File : OneNet_Post.py # @Note : https://blog.csdn.net/lishan132/article/details/114044902 import urllib.request import json # 设备ID、Key deviceId = "1077077815" APIKey = "N6Qfkvs2CS4xYq7jX3Zj4vgsl3g=" # 上传函数 def OneNet_post_data(info): url = "https://api.heclouds.com/devices/" + deviceId + '/datapoints' streams = [] for index, element in enumerate(info): streams.append({"id": element[0], "datapoints": [{"value": element[1]}]}) values = {"datastreams": streams} data = json.dumps(values).encode("utf-8") request = urllib.request.Request(url, data) request.add_header('api-key', APIKey) request.get_method = lambda: 'POST' request = urllib.request.urlopen(request) print(json.loads(request.read())) if __name__ == '__main__': upload_data = [ ["road1", 10], ["road2", 20], ["road3", 30], ] OneNet_post_data(upload_data)
标签:__,onnet,OneNet,python,request,urllib,streams,上报,data From: https://www.cnblogs.com/gooutlook/p/17378202.html