Aug-28-2023, 12:49 AM
I'm trying to develop an application split into two parts, server and client, that performs the same action on multiple devices at the same time. All devices are mine and friends. The aim is to control browser game accounts by doing the same action at the same time in the absence of one of us.
This is my first code, it just sends a mouse position and stops. I'm almost giving up.
If someone can help let me known.
This is my first code, it just sends a mouse position and stops. I'm almost giving up.
If someone can help let me known.
import socket
import threading
import tkinter as tk
from queue import Queue
import time
class ServerApp:
def __init__(self, root):
self.root = root
self.root.title("Server")
self.ip_entry = tk.Entry(root, width=15)
self.ip_entry.insert(0, "0.0.0.0")
self.ip_entry.pack()
self.port_entry = tk.Entry(root, width=5)
self.port_entry.insert(0, "80")
self.port_entry.pack()
self.connect_button = tk.Button(root, text="Conect", command=self.start_server)
self.connect_button.pack()
self.status_label = tk.Label(root, text="Status: Desconnected")
self.status_label.pack()
self.connected_clients_label = tk.Label(root, text="Clients conecteds:")
self.connected_clients_label.pack()
self.connected_clients_text = tk.Text(root, height=5, width=30)
self.connected_clients_text.pack()
self.server_on_button = tk.Button(root, text="Server On", command=self.toggle_server)
self.server_on_button.pack()
self.clients = []
self.mouse_x = 0
self.mouse_y = 0
self.pressed_keys = set()
self.is_server_on = False
self.root.bind("<Button-1>", self.send_left_click)
self.root.bind("<Key>", self.send_key_press)
self.root.bind("<Motion>", self.update_mouse_position)
self.root.bind("<KeyPress>", self.update_pressed_keys)
self.root.bind("<KeyRelease>", self.update_released_keys)
self.root.protocol("WM_DELETE_WINDOW", self.on_close)
self.update_clients_queue = Queue()
self.update_clients_thread = threading.Thread(target=self.update_connected_clients)
self.update_clients_thread.daemon = True
def on_close(self):
self.is_server_on = False
for client in self.clients:
client.close()
if self.listen_thread:
self.listen_thread.join()
self.root.destroy()
def start_server(self):
host = self.ip_entry.get()
port = int(self.port_entry.get())
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.bind((host, port))
self.server_socket.listen()
self.status_label.config(text="Status: Conectado")
self.connect_button.config(state=tk.DISABLED)
self.listen_thread = threading.Thread(target=self.listen_for_clients)
self.listen_thread.start()
self.is_server_on = False
self.update_clients_thread.start()
def listen_for_clients(self):
while True:
client_socket, client_addr = self.server_socket.accept()
self.clients.append(client_socket)
self.update_clients_queue.put(client_addr[0])
def update_connected_clients(self):
while True:
if not self.is_server_on:
break
if not self.update_clients_queue.empty():
client_addr = self.update_clients_queue.get()
self.connected_clients_text.insert(tk.END, f"{client_addr}\n")
self.update_clients_queue.task_done()
time.sleep(2)
def toggle_server(self):
self.is_server_on = not self.is_server_on
if self.is_server_on:
self.server_on_button.config(text="Server Off")
self.transmit_commands()
else:
self.server_on_button.config(text="Server On")
def transmit_commands(self):
prev_mouse_position = None
prev_pressed_keys = set()
while self.is_server_on:
mouse_position = f"{self.mouse_x},{self.mouse_y}"
pressed_keys_str = " ".join(self.pressed_keys)
if (
mouse_position != prev_mouse_position
or self.pressed_keys != prev_pressed_keys
):
for client in self.clients:
try:
command = f"server_on:{mouse_position}:{pressed_keys_str}"
print("Command send:", command)
client.send(command.encode())
except Exception as e:
print(f"Error on sending client command: {e}")
break
prev_mouse_position = mouse_position
prev_pressed_keys = self.pressed_keys
time.sleep(0.5)
def send_left_click(self, event):
for client in self.clients:
try:
client.send("left_click".encode())
except Exception as e:
print(f"Error on sending client command: {e}")
def send_key_press(self, event):
key = event.char
for client in self.clients:
try:
client.send(f"key_press:{key}".encode())
except Exception as e:
print(f"Error on sending client command: {e}")
def update_mouse_position(self, event):
self.mouse_x = event.x
self.mouse_y = event.y
def update_pressed_keys(self, event):
self.pressed_keys.add(event.char)
def update_released_keys(self, event):
self.pressed_keys.discard(event.char)
if __name__ == "__main__":
root = tk.Tk()
app = ServerApp(root)
root.mainloop()
