-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
136 lines (111 loc) · 3.12 KB
/
server.py
File metadata and controls
136 lines (111 loc) · 3.12 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
import sys
import asyncio
import websockets
import http.server
import socketserver
from threading import Thread
import socket
import json
from pynput.keyboard import Key, Controller,KeyCode
import time
import qrcode
PORT = 8000
WSPORT = 8765
# https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
keyCodeMap = {
37:Key.left,
38:Key.up,
39:Key.right,
40:Key.down,
13:Key.enter,
9: Key.tab,
90:KeyCode.from_char("z"), # btn a . Z
88:KeyCode.from_char("x"), # btn b . X
65:KeyCode.from_char("a"), # btn turbo a . A
83:KeyCode.from_char("s"), # btn turbo b. S
}
keyboard = Controller()
def pressKeyboardCode(key, isKyeUp=False):
global keyCodeMap
# print(f"{key} {isKyeUp}")
vk = keyCodeMap[key]
if isKyeUp:
keyboard.release(vk)
else:
keyboard.press(vk)
st = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
st.connect(('8.8.8.8', 1))
local_ip = st.getsockname()[0]
except Exception:
local_ip = '127.0.0.1'
finally:
st.close()
# = = = = = = == ==
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(f"http://{local_ip}:{PORT}")
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.show()
# = = = = = = == ==
keyState = {}
class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
# self.path = f'index.html'
self.path = f'v2.html'
return http.server.SimpleHTTPRequestHandler.do_GET(self)
async def echo(websocket):
global keyState
try:
async for message in websocket:
keyState = json.loads(message)
await websocket.send("1")
except:
pass
def monitor_thread():
handler_object = MyHttpRequestHandler
try:
with socketserver.TCPServer(("", PORT), handler_object) as httpd:
print( f"WEB SERVER STARTED\n http://{local_ip}:{PORT}" )
httpd.serve_forever()
except:
pass
def keydown_thread():
global keyState
procesed={}
try:
while True:
for k, v in keyState.items():
key = int(k)
isEvent = False
if k in procesed:
p_v = procesed[k]
if p_v != v or v == 1:
procesed[k] = v
isEvent = True
else:
if v == 1:
isEvent = True
procesed[k] = v
if isEvent:
pressKeyboardCode(key, v == 0 )
time.sleep(0.1)
except:
pass
async def main():
Thread(target=monitor_thread, daemon=True).start()
Thread(target=keydown_thread, daemon=True).start()
try:
async with websockets.serve(echo, "", WSPORT):
print( f"WS SERVER STARTED\n ws://{local_ip}:{WSPORT}" )
await asyncio.Future() # run forever
except:
pass
asyncio.run(main())