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