1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import os
- import sys
- from contextlib import closing
- # from base import init_env
- #
- # os.environ.setdefault("DJANGO_SETTINGS_MODULE", "configs.production")
- #
- # init_env(interactive = False)
- #
- # from apps.web.device.models import Group
- from gevent import socket
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'configs.testing')
- PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
- sys.path.insert(0, PROJECT_ROOT)
- from script.base import init_env, get_logger
- init_env(interactive = False)
- logger = get_logger(__name__)
- import simplejson as json
- # 启动2个线程,一个线程用于维持心跳,一个线程专门用于监听并处理事件。每分钟一次心跳,如果心跳失败,就重新建立连接。
- #
- server = '127.0.0.1'
- port = 8768
-
- def send_via_tcpip(server, port, message, timeout=0):
- # type:(str, int, dict)->dict
- payload = json.dumps(message)
- result = {"cmd": message["cmd"], "IMEI": str(message["IMEI"]), "rst": -1}
- try:
- with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
- if timeout:
- s.settimeout(timeout)
- s.connect((str(server), int(port)))
- s.sendall(payload)
- if timeout:
- result = json.loads(s.recv(2048))
- else:
- return {"cmd": message["cmd"], "IMEI": str(message["IMEI"]), "rst": 0}
-
- logger.info(
- '[old device(devNo=%s)] request device manager success. result = %s' %
- (message['IMEI'], json.dumps(result)))
- except Exception, e:
- logger.exception('[old device(devNo=%s)] request device manager exception. exception = %s' %
- (message['IMEI'], e))
- return result
- # 回复设备登录成功 {"cmd":"01","sqNo":"0000","IMEI":"55031412782305","data":"680C000000025503141278230500"}
- # 读取离线统计数据 {"cmd":"01","sqNo":"1A03","IMEI":"55031412782305","data":"680C1A0300125503141278230501"}
- try:
- while True:
- try:
- strSendMsg = raw_input(u"请输入发送给设备的消息: ") #
- timeout = raw_input(u"请输入超时时间: ")
- jsonData = json.loads(strSendMsg)
- send_via_tcpip(server, port, jsonData,int(timeout))
- except Exception,e:
- continue
-
- except Exception as ex:
- print(ex)
- print 'I am OVER'
|