-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyWebSocket.py
36 lines (35 loc) · 1.33 KB
/
MyWebSocket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import websocket
import base64
import time
class websocketInstance:
def __init__(self, url) -> None:
self.message_number = 0
self.initStr = [
base64.b16decode('7b2241757468546f6b656e223a22227d'.upper()),
base64.b16decode('317b22636f6c756d6e73223a3133382c22726f7773223a36317d'.upper())
]
self.headers = {
'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',
'Sec-WebSocket-Key': 'oYuZHR5Mj/VB9irNeNyMeA==',
'Sec-WebSocket-Protocol': 'tty',
'Sec-WebSocket-Version': '13',
'Upgrade': 'websocket'
}
self.ws = websocket.WebSocketApp('ws://{}.proxy.nscc-gz.cn:8888/ws'.format(url), header=self.headers, on_open = self.on_open, on_message= self.on_message, on_error=self.on_error)
def on_error(self, ws, error):
print(error)
ws.close()
def on_open(self, ws):
# print(' Websocket: open')
for char in self.initStr:
# print(char)
ws.send(char)
def on_message(self, ws, message):
self.message_number += 1
# print(message)
# print(self.message_number)
if self.message_number >= 3:
self.ws.close()
# print(" WebSocket: close")
def run(self):
self.ws.run_forever()