smart360.net的api使用 - python

发布时间:2016-12-8 20:00:30 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"smart360.net的api使用 - python",主要涉及到smart360.net的api使用 - python方面的内容,对于smart360.net的api使用 - python感兴趣的同学可以参考一下。

# -*- coding: utf-8 -*- import socket import time import struct import random import sys MASTER_HOST = sys.argv[1]   # 服务器ip MASTER_PORT = 5615          # 服务器port APPID       = sys.argv[2]   # appid class CMD(object):     CLIENT_JOIN                 = 0x01  # 连接结点     CLIENT_DATA                 = 0x02  # 传输数据     CLIENT_REDIRECT             = 0x03  # 连接转向     ERROR                       = 0xFE  # 错误(回应)     OK                          = 0xFC  # 正确(回应) class DEV(object):     SEND                        = 0x01  # 发射器     RECV                        = 0x02  # 接听器 # 获取appid所在的服务器      def get_host(ip, port, dev, appid):     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)     sock.connect((ip, port))     buf = struct.pack("!BB16s", CMD.CLIENT_JOIN, dev, appid)            # 请求加入appid包:命令字,设备类型,appid     sock.send(buf)                                                      # 发送申请加入appid     buf = sock.recv(255)     proto, = struct.unpack("!B", buf[0])     if proto == CMD.CLIENT_REDIRECT:                                    # 服务器收到申请后,会找一台与客户端最近的连接机,并返回其ip与port         proto, host = struct.unpack("!B%ds" % (len(buf) - 1), buf)         host = host.split("_")     elif proto == CMD.ERROR:         host = ["", 0]     else:         print "ERROR: proto %d" % proto     sock.close()     return host[0], int(host[1]) if __name__ == '__main__':     ip, port = get_host(MASTER_HOST, MASTER_PORT, DEV.SEND, APPID)     print "redirect to :" ,ip, port     if ip != "" and port != 0:         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)         sock.connect((ip, port))         buf = struct.pack("!BB16s", CMD.CLIENT_JOIN, DEV.SEND, APPID)           # 请求加入appid包:命令字,设备类型,appid         sock.send(buf)                                                          # 发送申请加入appid         buf = sock.recv(255)         proto, = struct.unpack("!B", buf[0])         if proto == CMD.OK:             while 1:                 buf = str(random.random()) + '\n'                 buf = struct.pack("!B%ds" % len(buf), CMD.CLIENT_DATA, buf)     # 发送数据包:命令字,数据                 sock.send(buf)                 buf = sock.recv(255)                 proto, = struct.unpack("!B", buf[0])                            # 发送是否成功确认                 if proto == CMD.OK:                     pass                 else:                     print "error"                 time.sleep(1)         else:             print "error in connect to your appid"         sock.close()     else:         print "bad connect!"   # -*- coding: utf-8 -*- import socket,time import struct import random import sys MASTER_HOST = sys.argv[1]   # 服务器ip MASTER_PORT = 5615          # 服务器port APPID       = sys.argv[2]   # appid class CMD(object):     CLIENT_JOIN                 = 0x01  # 连接结点     CLIENT_DATA                 = 0x02  # 传输数据     CLIENT_REDIRECT             = 0x03  # 连接转向     ERROR                       = 0xFE  # 错误(回应)     OK                          = 0xFC  # 正确(回应)     class DEV(object):     SEND                        = 0x01  # 发射器     RECV                        = 0x02  # 接听器     # 获取appid所在的服务器      def get_host(ip, port, dev, appid):     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)     sock.connect((ip, port))     buf = struct.pack("!BB16s", CMD.CLIENT_JOIN, dev, appid)            # 请求加入appid包:命令字,设备类型,appid     sock.send(buf)                                                      # 发送申请加入appid     buf = sock.recv(255)     proto, = struct.unpack("!B", buf[0])     if proto == CMD.CLIENT_REDIRECT:                                    # 服务器收到申请后,会找一台与客户端最近的连接机,并返回其ip与port         proto, host = struct.unpack("!B%ds" % (len(buf) - 1), buf)         host = host.split("_")     elif proto == CMD.ERROR:         host = ["", 0]     else:         print "ERROR: proto %d" % proto     sock.close()     return host[0], int(host[1]) if __name__ == '__main__':        ip, port = get_host(MASTER_HOST, MASTER_PORT, DEV.RECV, APPID)     print "redirect to :" ,ip, port     if ip != "" and port != 0:         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)         sock.connect((ip, port))         buf = struct.pack("!BB16s", CMD.CLIENT_JOIN, DEV.RECV, APPID)  # 请求加入appid包:命令字,设备类型,appid         sock.send(buf)                                                 # 发送申请加入appid         buf = sock.recv(255)         proto, = struct.unpack("!B", buf[0])         if proto == CMD.OK:                                            # 成功加入appid             while 1:                 buf = sock.recv(255)                 if len(buf) == 0:                     break                 sys.stdout.write(buf);                                 # 不断地从服务器接收数据         else:             print "error in connect to your appid"         sock.close()     else:         print "bad connect!"    

上一篇:给一个int型整数,如何将这个整数的奇偶位互换
下一篇:Linux下patch打补丁命令

相关文章

相关评论