-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·86 lines (70 loc) · 2.29 KB
/
server.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
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
#!/usr/bin/env python
import socket
import threading
from sys import platform
from os import system
###GLOBAL CONSTANTS###
SYSTEM = platform
HEADER = 8
if SYSTEM == "linux":
SERVER = str(system("ip a|grep 'state UP' -A2|tail -n1|awk '{print $2}'|cut -f1 -d'/'"))
elif SYSTEM == "darwin":
SERVER = str(system("ifconfig | grep 'inet ' | grep -Fv 127.0.0.1 | awk '{print $2}' | grep 192.168"))
PORT = 5050
ADDRESS = (SERVER, PORT)
FORMAT = "utf-8"
DISCONNECT_MESSAGE = "!exit"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
server.bind(ADDRESS)
except OSError as e:
print(e)
quit()
clients = []
def send(connection, msg):
msg = msg.encode(FORMAT)
msg_length = len(msg)
send_length_msg = str(msg_length).encode(FORMAT)
send_length_msg += b" " * (HEADER - len(send_length_msg))
connection.send(send_length_msg)
connection.send(msg)
def receive(connection):
msg_length = connection.recv(HEADER).decode(FORMAT)
if not msg_length:
return
msg_length = int(msg_length)
msg = connection.recv(msg_length).decode(FORMAT)
return msg
def handle_client(connection):
clients.append(connection)
name = receive(connection)
if '"' in name or "'" in name:
send(connection, "Names cannot contain quotes.")
connection.close()
return
send(connection, "Successfully connected to server! Type '!exit' to disconnect.")
for client in clients:
if client != connection:
send(client, f"[{name} has connected.]")
connected = True
while connected:
msg = receive(connection)
if msg == DISCONNECT_MESSAGE or msg is None:
for client in clients:
if client != connection:
send(client, f"[{name} has disconnected.]")
connection.close()
clients.remove(connection)
connected = False
break
for client in clients:
if client != connection:
send(client, f"{name}> {msg}")
def start_server():
server.listen()
print(f"[INFO] Starting server on:{SERVER}, listening on port:{PORT}")
while True:
connection, address = server.accept()
thread = threading.Thread(target=handle_client, args=(connection,))
thread.start()
start_server()