From fa6a0e769578b7b875c6ca2d1c33a561564e3f15 Mon Sep 17 00:00:00 2001 From: cube Date: Thu, 5 Feb 2026 21:11:59 +0000 Subject: [PATCH] client and server --- client.py | 72 +++++++++++++++++++++++++++++++++++++++++++ server.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 client.py create mode 100644 server.py diff --git a/client.py b/client.py new file mode 100644 index 0000000..806583c --- /dev/null +++ b/client.py @@ -0,0 +1,72 @@ +import socket, sys, datetime, threading, json + +class Client: + def __init__(self): + self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + print("Created socket object") + + self.client_name = "CLI" + + self.server_ip = "cubes.link" + self.server_port = 1337 + self.server_address = (self.server_ip, self.server_port) + + a = {"header":"connection", + "client name":self.client_name} + self.send(a, self.server_address) + + def send(self, data, addr): + data = json.dumps(data).encode() + self.sock.sendto(data, addr) + + def run(self): + while True: + data, addr = self.sock.recvfrom(65507) + a = json.loads(data) + header = a["header"] + + if header == "response": + message = a["message"] + client_name = a["client name"] + print(client_name + " >> " + message) + + elif header == "connection": + motd = a["motd"] + client_name = a["client name"] + print(">> " + client_name + " <<") + +class Commands: + def __init__(self): + self.commands={"test":self.test, + "stop":self.stop} + self.running = True + + def test(self, params): + print("this is a test command") + + def stop(self, params): + print("this doesn't do anything rn") + + def run(self): + while self.running: + text = input() + if text[0] == "/": + parts = text.split(" ") + command = parts[0].strip("/") + params = parts.pop(0) + if command in self.commands: + self.commands[command](params) + else: + a = {"header":"message", + "message":text, + "client name":client.client_name} + client.send(a, client.server_address) + +client = Client() +commands = Commands() + +client_thread = threading.Thread(target = client.run) +commands_thread = threading.Thread(target = commands.run) + +client_thread.start() +commands_thread.start() \ No newline at end of file diff --git a/server.py b/server.py new file mode 100644 index 0000000..22faae4 --- /dev/null +++ b/server.py @@ -0,0 +1,91 @@ +import socket, sys, datetime, threading, json + +class Server: + def __init__(self): + self.version = 1.0 + print("Server version: {}".format(self.version)) + self.motd = "cubey" + self.color = 0xe973ea + self.users = [] + + self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + print("Created socket object") + + self.ip = "0.0.0.0" + self.port = 1337 + self.address = (self.ip, self.port) + self.sock.bind(self.address) + print("Server started up on {}".format(self.address)) + + def send(self, data, client): + data = json.dumps(data).encode() + self.sock.sendto(data, client) + + def send_all(self, data): + for user in self.users: + self.send(data, user) + + def connection(self, address, connecting_client_name): + print("New connection from {}".format(address)) + a = {"header":"connection", + "motd":self.motd, + "color":self.color, + "client name":connecting_client_name} + print(">> " + connecting_client_name + " <<") + self.users.append(address) + self.send_all(a) + + def run(self): + while True: + try: + data, addr = self.sock.recvfrom(65507) + a = json.loads(data) + header = a["header"] + client_name = a["client name"] + + if header == "connection": + self.connection(addr, client_name) + elif header == "message": + message = a["message"] + print(client_name + " >> " + message) + response = {"header":"response", + "message":message, + "color":0xffffff, + "client name":client_name} + if addr not in self.users: + self.users.append(addr) + self.send_all(response) + except OSError: + return + +class Commands: + def __init__(self): + self.commands={"test":self.test, + "stop":self.stop} + self.running = True + + def test(self): + print("this is a test command") + + def stop(self): + print("Stopping server...") + server.sock.close() + server_thread.join() + self.running = False + + def run(self): + while self.running: + command = input() + if command in self.commands: + self.commands[command]() + else: + print("Unknown command") + +server = Server() +commands = Commands() + +server_thread = threading.Thread(target = server.run) +commands_thread = threading.Thread(target = commands.run) + +server_thread.start() +commands_thread.start() \ No newline at end of file