All files

This commit is contained in:
2025-11-04 08:19:56 +01:00
commit 059d668618
9 changed files with 655 additions and 0 deletions

96
client.py Normal file
View File

@@ -0,0 +1,96 @@
import socket
import sys
import threading
import json
from datetime import datetime, timezone
def listen_to_server(sock):
buffer = ""
try:
while True:
data = sock.recv(4096)
if not data:
print("🔌 Connection closed by server.")
break
buffer += data.decode()
while '\n' in buffer:
line, buffer = buffer.split('\n', 1)
try:
msg = json.loads(line)
ts = msg.get("timestamp", "?")
sender = msg.get("sender", "?")
content = msg.get("content", "")
level = msg.get("level", "local")
print(f"[Received @ {ts}] {sender} ({level}): {content}")
except json.JSONDecodeError:
print("⚠Received invalid JSON.")
except Exception as e:
print("⚠Error receiving from server:", e)
finally:
sock.close()
print("Disconnected.")
sys.exit(0)
if __name__ == "__main__":
if len(sys.argv) != 5:
print("Usage: python client.py <username> <region> <host> <port>")
sys.exit(1)
username = sys.argv[1]
region = sys.argv[2]
host = sys.argv[3]
port = int(sys.argv[4])
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
except ConnectionRefusedError:
print(f"Connection refused to {host}:{port}. Is the server running?")
sys.exit(1)
except Exception as e:
print(f"Connection error: {e}")
sys.exit(1)
print(f"Connected to server {host}:{port} as {username}")
print("Type messages. Use `::level=local|national|global` (default: local)")
threading.Thread(target=listen_to_server, args=(sock,), daemon=True).start()
try:
while True:
line = input()
if "::level=" in line:
parts = line.split("::level=")
content = parts[0].strip()
level = parts[1].strip().lower()
else:
content = line.strip()
level = "local"
if not content:
continue
timestamp = datetime.now(timezone.utc).isoformat()
message = {
"id": f"{username}-{timestamp}",
"sender": username,
"content": content,
"level": level,
"timestamp": timestamp,
"region": region
}
try:
sock.sendall((json.dumps(message) + "\n").encode())
except Exception:
print("Failed to send message. Disconnected?")
break
except KeyboardInterrupt:
print("\nClient closed.")
finally:
sock.close()
sys.exit(0)