PC+python 上传例程

2012-11-12 14:06

 

 

 
#感谢grissiom 提供的代码

 

 

 

# encoding: utf-8

 

import socket, serial, time

 

HOST = "open.lewei50.com"

HOST = "223.5.16.122"

PORT = 80

 

user_key = '844d1f1d35ab454f87669fa9795a4805'

 

post_sch = "POST /api/V1/Gateway/UpdateSensors/01 HTTP/1.1 " +

         "Host: open.lewei50.com " +

         "Content-Length: {msg_len} " +

         "userkey: %s " % user_key +

         " " +

         "{msg}"

 

def send_data(msg):

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        s.settimeout(None)

        s.connect((HOST, PORT))

 

        post_data = post_sch.format(msg_len=len(msg), msg=msg)

 

        print '==== send ==== '

        print post_data

        s.send(post_data)

 

        time.sleep(5)

 

        re = s.recv(20000, socket.MSG_DONTWAIT)

        print '==== recv ===='

        print re

 

        s.close()

 

send_data('[{"Name":"T1","Value":"20"}, {"Name":"T2", "Value":"170"}]')