kwebsocket 1.2__tar.gz → 1.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {kwebsocket-1.2 → kwebsocket-1.4}/PKG-INFO +2 -2
- {kwebsocket-1.2 → kwebsocket-1.4}/kwebsocket/__init__.py +1 -1
- kwebsocket-1.4/kwebsocket/rfdszbfnbxrdfgvxrhbdtxrhtbs.py +232 -0
- {kwebsocket-1.2 → kwebsocket-1.4}/kwebsocket.egg-info/PKG-INFO +2 -2
- {kwebsocket-1.2 → kwebsocket-1.4}/kwebsocket.egg-info/requires.txt +1 -0
- {kwebsocket-1.2 → kwebsocket-1.4}/setup.py +1 -1
- kwebsocket-1.2/kwebsocket/rfdszbfnbxrdfgvxrhbdtxrhtbs.py +0 -126
- {kwebsocket-1.2 → kwebsocket-1.4}/kwebsocket.egg-info/SOURCES.txt +0 -0
- {kwebsocket-1.2 → kwebsocket-1.4}/kwebsocket.egg-info/dependency_links.txt +0 -0
- {kwebsocket-1.2 → kwebsocket-1.4}/kwebsocket.egg-info/top_level.txt +0 -0
- {kwebsocket-1.2 → kwebsocket-1.4}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 1.2
|
|
2
2
|
Name: kwebsocket
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.4
|
|
4
4
|
Summary: kwebsocket
|
|
5
5
|
Home-page: UNKNOWN
|
|
6
6
|
Author: 百里
|
|
@@ -9,5 +9,5 @@ Maintainer: 坤坤
|
|
|
9
9
|
Maintainer-email: fk1402936534@qq.com
|
|
10
10
|
License: MIT License
|
|
11
11
|
Description: UNKNOWN
|
|
12
|
-
Keywords: kwebsocket1.
|
|
12
|
+
Keywords: kwebsocket1.4
|
|
13
13
|
Platform: UNKNOWN
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import time,threading,hashlib,json,os
|
|
3
|
+
def kwebsocket_print_log(*strs):
|
|
4
|
+
print(time.strftime("%Y-%m-%d %H:%M:%S"),*strs)
|
|
5
|
+
def kwebsocket_json_encode(strs):
|
|
6
|
+
"""python列表或字典转成字符串"""
|
|
7
|
+
try:
|
|
8
|
+
return json.dumps(strs,ensure_ascii=False)
|
|
9
|
+
except Exception:
|
|
10
|
+
return ""
|
|
11
|
+
def kwebsocket_md5(strs):
|
|
12
|
+
"""md5加密
|
|
13
|
+
|
|
14
|
+
参数 strs:要加密的字符串
|
|
15
|
+
|
|
16
|
+
return String类型
|
|
17
|
+
"""
|
|
18
|
+
m = hashlib.md5()
|
|
19
|
+
b = strs.encode(encoding='utf-8')
|
|
20
|
+
m.update(b)
|
|
21
|
+
return m.hexdigest()
|
|
22
|
+
def kwebsocket_is_index(params,index):
|
|
23
|
+
try:
|
|
24
|
+
params[index]
|
|
25
|
+
except KeyError:
|
|
26
|
+
return False
|
|
27
|
+
except IndexError:
|
|
28
|
+
return False
|
|
29
|
+
else:
|
|
30
|
+
return True
|
|
31
|
+
def file_get_content(filename,cur_encoding='utf-8'):
|
|
32
|
+
"""获取文件内容
|
|
33
|
+
|
|
34
|
+
filename 完整文件名
|
|
35
|
+
|
|
36
|
+
cur_encoding 指定编码获取文件内容
|
|
37
|
+
|
|
38
|
+
encoding 是否返回文件编码 默认否
|
|
39
|
+
"""
|
|
40
|
+
fileData=''
|
|
41
|
+
if os.path.isfile(filename):
|
|
42
|
+
with open(filename,encoding=cur_encoding) as file:
|
|
43
|
+
fileData = file.read()
|
|
44
|
+
return fileData
|
|
45
|
+
class vsregrsgtrdhbrhtrsgrshydtrsegregsresgr:
|
|
46
|
+
wsobj={}
|
|
47
|
+
__config={
|
|
48
|
+
'debug':False,
|
|
49
|
+
'th_lock':False,
|
|
50
|
+
'url':'',
|
|
51
|
+
'break':5 #断线重连次数,0表示不重连
|
|
52
|
+
}
|
|
53
|
+
__thlock={
|
|
54
|
+
'obj':None,
|
|
55
|
+
'status':False
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
__tempwsobj=None
|
|
59
|
+
__bs=None
|
|
60
|
+
__def_url=''
|
|
61
|
+
def __connect(self):
|
|
62
|
+
if not self.__config['url']:
|
|
63
|
+
if os.name == 'posix':
|
|
64
|
+
if not self.__def_url:
|
|
65
|
+
try:
|
|
66
|
+
php_ims_datassss=json.loads(file_get_content(os.getcwd()+'/kwebsp/app/official/controller/phpims/common/file/sqlite/data'))
|
|
67
|
+
sockets_admin_authkey=php_ims_datassss['admintoken']
|
|
68
|
+
except:
|
|
69
|
+
import kuncache
|
|
70
|
+
php_ims_datassss=''
|
|
71
|
+
sockets_admin_authkey=kuncache.cache.set_config({'type':'File','path':'/kwebsp/app/runtime/cachepath'}).get_cache("admin_authkey")
|
|
72
|
+
if not sockets_admin_authkey:
|
|
73
|
+
raise Exception('连接地址不存在')
|
|
74
|
+
|
|
75
|
+
if php_ims_datassss:
|
|
76
|
+
self.__def_url='ws://'+php_ims_datassss['domain']+'/?admintoken='+sockets_admin_authkey
|
|
77
|
+
else:
|
|
78
|
+
self.__def_url="ws://127.0.0.1:39030?authkey="+sockets_admin_authkey
|
|
79
|
+
self.__config['url']=self.__def_url
|
|
80
|
+
else:
|
|
81
|
+
raise Exception('连接地址不存在')
|
|
82
|
+
|
|
83
|
+
self.__bs=kwebsocket_md5(self.__config['url'])
|
|
84
|
+
if not kwebsocket_is_index(self.wsobj,self.__bs):
|
|
85
|
+
import websocket
|
|
86
|
+
for i in range(100):
|
|
87
|
+
self.wsobj[self.__bs] = websocket.WebSocket()
|
|
88
|
+
try:
|
|
89
|
+
self.wsobj[self.__bs].connect(self.__config['url'])
|
|
90
|
+
except websocket._exceptions.WebSocketBadStatusException as e:
|
|
91
|
+
self.close()
|
|
92
|
+
if i>=self.__config['break']:
|
|
93
|
+
self.__close_th_lock()
|
|
94
|
+
raise Exception('连接已中止,请重新连接')
|
|
95
|
+
except:
|
|
96
|
+
self.close()
|
|
97
|
+
self.__close_th_lock()
|
|
98
|
+
raise
|
|
99
|
+
else:
|
|
100
|
+
break
|
|
101
|
+
self.__tempwsobj=self.wsobj[self.__bs]
|
|
102
|
+
def __start_th_lock(self):
|
|
103
|
+
"""开启线程锁 多线程中建议开启 注意 这个python多线程锁 而不是数据库事务锁"""
|
|
104
|
+
if not self.__thlock['obj']:
|
|
105
|
+
self.__thlock['obj']=threading.Lock()
|
|
106
|
+
self.__thlock['obj'].acquire()
|
|
107
|
+
self.__thlock['status']=True
|
|
108
|
+
if self.__config['debug']:
|
|
109
|
+
kwebsocket_print_log('开启线程锁cache')
|
|
110
|
+
def __close_th_lock(self):
|
|
111
|
+
"""退出线程锁 这个python多线程锁 而不是数据库事务锁"""
|
|
112
|
+
if self.__thlock['status']:
|
|
113
|
+
self.__thlock['obj'].release()
|
|
114
|
+
self.__thlock['status']=False
|
|
115
|
+
if self.__config['debug']:
|
|
116
|
+
kwebsocket_print_log('退出线程锁cache')
|
|
117
|
+
def close(self):
|
|
118
|
+
if kwebsocket_is_index(self.wsobj,self.__bs):
|
|
119
|
+
try:self.wsobj[self.__bs].close()
|
|
120
|
+
except:pass
|
|
121
|
+
del self.wsobj[self.__bs]
|
|
122
|
+
self.__tempwsobj=None
|
|
123
|
+
def connect(self,url='',breaks=5,th_lock='no'):
|
|
124
|
+
"""设置连接
|
|
125
|
+
|
|
126
|
+
url 连接地址 如 wss://websocket.kwebapp.cn/?unionid=1&token=1232
|
|
127
|
+
|
|
128
|
+
breaks #断线重连次数,0表示不重连
|
|
129
|
+
|
|
130
|
+
th_lock 是否开启线程锁 多线程中建议开启 注意 这个python多线程锁
|
|
131
|
+
|
|
132
|
+
"""
|
|
133
|
+
if th_lock!='no':
|
|
134
|
+
self.__config['th_lock']=th_lock
|
|
135
|
+
if self.__config['th_lock']:
|
|
136
|
+
self.__start_th_lock()
|
|
137
|
+
self.__config['url']=url
|
|
138
|
+
self.__config['break']=breaks
|
|
139
|
+
return self
|
|
140
|
+
|
|
141
|
+
def send(self,text):
|
|
142
|
+
"""发送信息
|
|
143
|
+
|
|
144
|
+
text 要推送的数据 字符串格式
|
|
145
|
+
|
|
146
|
+
"""
|
|
147
|
+
for i in range(100):
|
|
148
|
+
self.__connect()
|
|
149
|
+
try:
|
|
150
|
+
self.__tempwsobj.send(text)
|
|
151
|
+
except ConnectionAbortedError:
|
|
152
|
+
self.close()
|
|
153
|
+
if i>=self.__config['break']:
|
|
154
|
+
self.__close_th_lock()
|
|
155
|
+
raise Exception('连接已中止,请重新连接')
|
|
156
|
+
time.sleep(0.1)
|
|
157
|
+
except BrokenPipeError:
|
|
158
|
+
self.close()
|
|
159
|
+
if i>=self.__config['break']:
|
|
160
|
+
self.__close_th_lock()
|
|
161
|
+
raise Exception('连接已中止,请重新连接')
|
|
162
|
+
time.sleep(0.1)
|
|
163
|
+
except:
|
|
164
|
+
self.close()
|
|
165
|
+
self.__close_th_lock()
|
|
166
|
+
raise
|
|
167
|
+
else:
|
|
168
|
+
self.__close_th_lock()
|
|
169
|
+
break
|
|
170
|
+
self.__tempwsobj=None
|
|
171
|
+
self.__config['url']=''
|
|
172
|
+
def send_arr(self,data,types,admintoken,GroupName='',client_id='',unionid=''):
|
|
173
|
+
"""发送信息
|
|
174
|
+
|
|
175
|
+
data 要推送的数据 支持字典
|
|
176
|
+
|
|
177
|
+
types 类型 sendToAll(给所有在线发送信息) sendToGroup(给指定组发送信息) sendToClient(给指定客户端发送信息) sendToUid(给指定客户端发送信息)
|
|
178
|
+
|
|
179
|
+
admintoken 管理员连接token
|
|
180
|
+
|
|
181
|
+
GroupName 组名 types=sendToGroup时必填
|
|
182
|
+
|
|
183
|
+
client_id 客户端id types=sendToClient时必填
|
|
184
|
+
|
|
185
|
+
unionid 用户id types=sendToUid时必填
|
|
186
|
+
|
|
187
|
+
"""
|
|
188
|
+
con={'type':types,'admintoken':admintoken}
|
|
189
|
+
if types=='sendToGroup':
|
|
190
|
+
if not GroupName:
|
|
191
|
+
raise Exception('GroupName not null')
|
|
192
|
+
con['GroupName']=GroupName
|
|
193
|
+
elif types=='sendToClient':
|
|
194
|
+
if not client_id:
|
|
195
|
+
raise Exception('client_id not null')
|
|
196
|
+
con['client_id']=client_id
|
|
197
|
+
elif types=='sendToUid':
|
|
198
|
+
if not unionid:
|
|
199
|
+
raise Exception('unionid not null')
|
|
200
|
+
con['unionid']=unionid
|
|
201
|
+
con['data']=data
|
|
202
|
+
self.send(kwebsocket_json_encode(con))
|
|
203
|
+
def recv(self):
|
|
204
|
+
"""接收数据"""
|
|
205
|
+
for i in range(100):
|
|
206
|
+
self.__connect()
|
|
207
|
+
try:
|
|
208
|
+
data = self.__tempwsobj.recv()
|
|
209
|
+
except ConnectionAbortedError:
|
|
210
|
+
self.close()
|
|
211
|
+
if i>=self.__config['break']:
|
|
212
|
+
self.__close_th_lock()
|
|
213
|
+
raise Exception('连接已中止,请重新连接')
|
|
214
|
+
time.sleep(0.1)
|
|
215
|
+
except BrokenPipeError:
|
|
216
|
+
self.close()
|
|
217
|
+
if i>=self.__config['break']:
|
|
218
|
+
self.__close_th_lock()
|
|
219
|
+
raise Exception('连接已中止,请重新连接')
|
|
220
|
+
time.sleep(0.1)
|
|
221
|
+
except:
|
|
222
|
+
self.close()
|
|
223
|
+
self.__close_th_lock()
|
|
224
|
+
raise
|
|
225
|
+
else:
|
|
226
|
+
self.__close_th_lock()
|
|
227
|
+
break
|
|
228
|
+
self.__tempwsobj=None
|
|
229
|
+
self.__config['url']=''
|
|
230
|
+
return data
|
|
231
|
+
|
|
232
|
+
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 1.2
|
|
2
2
|
Name: kwebsocket
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.4
|
|
4
4
|
Summary: kwebsocket
|
|
5
5
|
Home-page: UNKNOWN
|
|
6
6
|
Author: 百里
|
|
@@ -9,5 +9,5 @@ Maintainer: 坤坤
|
|
|
9
9
|
Maintainer-email: fk1402936534@qq.com
|
|
10
10
|
License: MIT License
|
|
11
11
|
Description: UNKNOWN
|
|
12
|
-
Keywords: kwebsocket1.
|
|
12
|
+
Keywords: kwebsocket1.4
|
|
13
13
|
Platform: UNKNOWN
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
import time,threading
|
|
3
|
-
def kwebsocket_print_log(*strs):
|
|
4
|
-
print(time.strftime("%Y-%m-%d %H:%M:%S"),*strs)
|
|
5
|
-
class vsregrsgtrdhbrhtrsgrshydtrsegregsresgr:
|
|
6
|
-
wsobj=None
|
|
7
|
-
__config={
|
|
8
|
-
'debug':False,
|
|
9
|
-
'th_lock':False,
|
|
10
|
-
'url':'',
|
|
11
|
-
'break':5 #断线重连次数,0表示不重连
|
|
12
|
-
}
|
|
13
|
-
__thlock={
|
|
14
|
-
'obj':None,
|
|
15
|
-
'status':False
|
|
16
|
-
}
|
|
17
|
-
def __connect(self):
|
|
18
|
-
if not self.wsobj:
|
|
19
|
-
import websocket
|
|
20
|
-
for i in range(100):
|
|
21
|
-
self.wsobj = websocket.WebSocket()
|
|
22
|
-
try:
|
|
23
|
-
self.wsobj.connect(self.__config['url'])
|
|
24
|
-
except websocket._exceptions.WebSocketBadStatusException:
|
|
25
|
-
try:self.wsobj.close()
|
|
26
|
-
except:pass
|
|
27
|
-
self.wsobj=None
|
|
28
|
-
if i>=self.__config['break']:
|
|
29
|
-
self.__close_th_lock()
|
|
30
|
-
raise Exception('连接已中止,请重新连接')
|
|
31
|
-
time.sleep(0.1)
|
|
32
|
-
except:
|
|
33
|
-
try:self.wsobj.close()
|
|
34
|
-
except:pass
|
|
35
|
-
self.wsobj=None
|
|
36
|
-
self.__close_th_lock()
|
|
37
|
-
raise
|
|
38
|
-
else:
|
|
39
|
-
break
|
|
40
|
-
def __start_th_lock(self):
|
|
41
|
-
"""开启线程锁 多线程中建议开启 注意 这个python多线程锁 而不是数据库事务锁"""
|
|
42
|
-
if not self.__thlock['obj']:
|
|
43
|
-
self.__thlock['obj']=threading.Lock()
|
|
44
|
-
self.__thlock['obj'].acquire()
|
|
45
|
-
self.__thlock['status']=True
|
|
46
|
-
if self.__config['debug']:
|
|
47
|
-
kwebsocket_print_log('开启线程锁cache')
|
|
48
|
-
def __close_th_lock(self):
|
|
49
|
-
"""退出线程锁 这个python多线程锁 而不是数据库事务锁"""
|
|
50
|
-
if self.__thlock['status']:
|
|
51
|
-
self.__thlock['obj'].release()
|
|
52
|
-
self.__thlock['status']=False
|
|
53
|
-
if self.__config['debug']:
|
|
54
|
-
kwebsocket_print_log('退出线程锁cache')
|
|
55
|
-
def close(self):
|
|
56
|
-
if self.wsobj:
|
|
57
|
-
try:self.wsobj.close()
|
|
58
|
-
except:pass
|
|
59
|
-
self.wsobj=None
|
|
60
|
-
def connect(self,url,th_lock=False):
|
|
61
|
-
"""设置连接
|
|
62
|
-
|
|
63
|
-
url 连接地址 如 wss://websocket.kwebapp.cn/?unionid=1&token=1232
|
|
64
|
-
|
|
65
|
-
th_lock 是否开启线程锁 多线程中建议开启 注意 这个python多线程锁 而不是redis库事务锁 也可以在配置信息中全局开启
|
|
66
|
-
|
|
67
|
-
"""
|
|
68
|
-
if th_lock!='no':
|
|
69
|
-
self.__config['th_lock']=th_lock
|
|
70
|
-
if self.__config['th_lock']:
|
|
71
|
-
self.__start_th_lock()
|
|
72
|
-
self.__config['url']=url
|
|
73
|
-
return self
|
|
74
|
-
|
|
75
|
-
def send(self,text):
|
|
76
|
-
for i in range(100):
|
|
77
|
-
self.__connect()
|
|
78
|
-
try:
|
|
79
|
-
self.wsobj.send(text)
|
|
80
|
-
except ConnectionAbortedError:
|
|
81
|
-
self.close()
|
|
82
|
-
if i>=self.__config['break']:
|
|
83
|
-
self.__close_th_lock()
|
|
84
|
-
raise Exception('连接已中止,请重新连接')
|
|
85
|
-
time.sleep(0.1)
|
|
86
|
-
except BrokenPipeError:
|
|
87
|
-
self.close()
|
|
88
|
-
if i>=self.__config['break']:
|
|
89
|
-
self.__close_th_lock()
|
|
90
|
-
raise Exception('连接已中止,请重新连接')
|
|
91
|
-
time.sleep(0.1)
|
|
92
|
-
except:
|
|
93
|
-
self.close()
|
|
94
|
-
self.__close_th_lock()
|
|
95
|
-
raise
|
|
96
|
-
else:
|
|
97
|
-
self.__close_th_lock()
|
|
98
|
-
break
|
|
99
|
-
def recv(self):
|
|
100
|
-
"""接收数据"""
|
|
101
|
-
for i in range(100):
|
|
102
|
-
self.__connect()
|
|
103
|
-
try:
|
|
104
|
-
data = self.wsobj.recv()
|
|
105
|
-
except ConnectionAbortedError:
|
|
106
|
-
self.close()
|
|
107
|
-
if i>=self.__config['break']:
|
|
108
|
-
self.__close_th_lock()
|
|
109
|
-
raise Exception('连接已中止,请重新连接')
|
|
110
|
-
time.sleep(0.1)
|
|
111
|
-
except BrokenPipeError:
|
|
112
|
-
self.close()
|
|
113
|
-
if i>=self.__config['break']:
|
|
114
|
-
self.__close_th_lock()
|
|
115
|
-
raise Exception('连接已中止,请重新连接')
|
|
116
|
-
time.sleep(0.1)
|
|
117
|
-
except:
|
|
118
|
-
self.close()
|
|
119
|
-
self.__close_th_lock()
|
|
120
|
-
raise
|
|
121
|
-
else:
|
|
122
|
-
self.__close_th_lock()
|
|
123
|
-
break
|
|
124
|
-
return data
|
|
125
|
-
|
|
126
|
-
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|