Commit fd81e3a9 by 潘建波

Merge branch 'develop' of http://192.168.9.26/platform/tx1_box_serv into develop

# Conflicts:
#	code/tx1_box_serv/apps/api/ws_serv.py
2 parents f98174f1 9e896ed0
#-*-coding:UTF-8-*-, # coding: utf-8
from socket import * from socket import *
import json, time, threading import json, time, threading
import gbvar.ws_serv_global as sv_global import apps.gbvar.ws_serv_global as sv_global
import sys
reload(sys)
sys.setdefaultencoding('UTF-8')
config = { config = {
'LISTEN_CLIENT': 50, 'LISTEN_CLIENT': 50,
'KEY': '391f10fadc339e9ec5fa15af60030ac1', 'KEY': '391f10fadc339e9ec5fa15af60030ac1',
...@@ -32,18 +29,19 @@ class Server (): ...@@ -32,18 +29,19 @@ class Server ():
self.sock.bind ( (h,p) ) # 监听端口 self.sock.bind ( (h,p) ) # 监听端口
#self.sock.bind ( (config['HOST'], config['PORT']) ) # 监听端口 #self.sock.bind ( (config['HOST'], config['PORT']) ) # 监听端口
self.sock.listen ( config['LISTEN_CLIENT'] ) # 监听客户端数量 self.sock.listen ( config['LISTEN_CLIENT'] ) # 监听客户端数量
self.sock.setsockopt(SOL_SOCKET, SO_LINGER, 1)
# 所有监听的客户端 # 所有监听的客户端
self.clients = {} self.clients = {}
self.sub_task_id = ''
self.thrs = {} self.thrs = {}
self.users = {} self.users = {}
self.stops = [] self.stops = []
self.h=h self.h=h
self.p=p self.p=p
self.subtask_id=None
# 监听客户端连接 # 监听客户端连接
def listen_client(self): def listen_client(self):
while 1: while 1:
# time.sleep(500)
# 循环监听 # 循环监听
tcpClientSock, addr = self.sock.accept () tcpClientSock, addr = self.sock.accept ()
address = addr[0] + ':' + str ( addr[1] ) # ip:port address = addr[0] + ':' + str ( addr[1] ) # ip:port
...@@ -130,10 +128,10 @@ class Server (): ...@@ -130,10 +128,10 @@ class Server ():
if not info: if not info:
continue continue
if info == 'quit': if info == 'quit':
self.close_client ( address ) self.close_client ( address )
break break
print(info)
code_len = ord ( info[1] ) & 127 code_len = ord ( info[1] ) & 127
if code_len == 126: if code_len == 126:
masks = info[4:8] masks = info[4:8]
...@@ -151,47 +149,40 @@ class Server (): ...@@ -151,47 +149,40 @@ class Server ():
raw_str += chr ( ord ( d ) ^ ord ( masks[i % 4] ) ) raw_str += chr ( ord ( d ) ^ ord ( masks[i % 4] ) )
# print(raw_str) # print(raw_str)
i += 1 i += 1
print raw_str
# 获取到输入的数据 向所有的客户端发送 # 获取到输入的数据 向所有的客户端发送
# 开启线程记录 # 开启线程记录
if raw_str: if raw_str:
print type(raw_str)
try: try:
cdata = eval(raw_str) da = eval(raw_str)
if(cdata.has_key('subtask_id')): if type(da) is dict:
self.sub_task_id = cdata['subtask_id'] if da.has_key('subtask_id'):
else: self.subtask_id = da['subtask_id']
return
except:
print(raw_str)
#t1 = threading.Thread ( target=self.send_data, args=[raw_str, address] )
#t1.start ()
def send_data(self, data, address):
try:
td = json.loads(data)
if type(td) is dict:
if (td.has_key('subtask_id')):
data = data
else:
data =''
else:
data = data
print(data)
except Exception as e: except Exception as e:
print e print e
def send_data(self, str_data, address):
#data = eval(data)
if type(str_data) is str:
data = json.loads (str_data)
if type(data) is dict:
if data.has_key('subtask_id'):
subtask_id = data['subtask_id']
if self.subtask_id and self.subtask_id == subtask_id:
import struct import struct
from urllib import unquote from urllib import unquote
try: try:
username = unquote ( self.users[address] ) username = unquote ( self.users[address] )
except: except:
username = '匿名用户' username = '匿名用户'
#if data: # if data:
# data = str ( '【' + username + '说】' + data ) # data = str ( '【' + username + '说】' + data )
#else: # else:
# return False # return False
token = "\x81" token = "\x81"
length = len ( data ) length = len ( str_data )
if length < 126: if length < 126:
print '126' print '126'
token += struct.pack ( "B", length ) token += struct.pack ( "B", length )
...@@ -204,10 +195,10 @@ class Server (): ...@@ -204,10 +195,10 @@ class Server ():
print 'token=%s' % token print 'token=%s' % token
# struct为Python中处理二进制数的模块,二进制流为C,或网络流的形式。 # struct为Python中处理二进制数的模块,二进制流为C,或网络流的形式。
data = '%s%s' % (token, data) str_data = '%s%s' % (token, str_data)
# print data print str_data
try: try:
#for key, val in self.clients.iteritems (): # for key, val in self.clients.iteritems ():
# print # print
# client = val # client = val
# try: # try:
...@@ -216,9 +207,15 @@ class Server (): ...@@ -216,9 +207,15 @@ class Server ():
# except: # except:
# self.close_client ( key ) # self.close_client ( key )
client = self.clients[address] client = self.clients[address]
client.send ( data ) client.send ( str_data )
except: except:
pass pass
else:
pass
else:
pass
else:
pass
def close_client(self, address): def close_client(self, address):
try: try:
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!