forked from Cat1007/yuketangHelperSCUTLite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_websockets.py
81 lines (72 loc) · 2.3 KB
/
get_websockets.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
# version 1
# developed by MuWinds
import json
import websocket
import threading
import time
import qrcode
import io
import os
class WebSocketQrcode:
def __init__(self):
self.fetch_qrcode_timer = None
self.ws = None
self.login_message=""
def on_message(self, ws, message):
msg = json.loads(message)
if 'ticket' in msg:
if(msg['qrcode']!=''):
print_qrcode(msg['qrcode'])
if msg.get('op') == 'requestlogin':
self.fetch_qrcode()
if msg.get('op') == 'loginsuccess':
self.login_message = message
#关闭连接
self.ws.close()
if self.fetch_qrcode_timer:
self.fetch_qrcode_timer.cancel()
def on_error(self, ws, error):
print("Error:", error)
def on_close(self, ws,close_status_code,bytestring):
print("")#关闭连接
def on_open(self, ws):
print("Connection opened")
self.fetch_qrcode()
self.fetch_qrcode_timer = threading.Timer(60, self.fetch_qrcode) # 50秒后刷新二维码
self.fetch_qrcode_timer.start()
def fetch_qrcode(self):
if self.ws:
self.ws.send(json.dumps({
'op': "requestlogin",
'role': "web",
'version': 1.4,
'type': "qrcode"
}))
def run(self,domain):
self.ws = websocket.WebSocketApp("wss://"+domain+"/wsapp/",
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close)
self.ws.on_open = self.on_open
self.ws.run_forever()
return self.login_message
def print_qrcode(qr_data):
# 生成二维码
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(qr_data)
qr.make(fit=True)
img = qr.make_image(fill='black', back_color='white')
# 将二维码图像保存到内存
img_buffer = io.BytesIO()
img.save(img_buffer, format='PNG')
img_buffer.seek(0)
# 打印二维码
os.system('cls' if os.name == 'nt' else 'clear') # 清屏
print("QRCode:")
img.show()