Compare commits
No commits in common. "17a76e4b90ddd1ece0c31cb4047281ed6cd2a957" and "5ef19b3673864937d620089658b764a717e91ccc" have entirely different histories.
17a76e4b90
...
5ef19b3673
40
server.py
40
server.py
@ -1,40 +0,0 @@
|
|||||||
import socket
|
|
||||||
import threading
|
|
||||||
|
|
||||||
class Connection:
|
|
||||||
sock=""
|
|
||||||
addr=0
|
|
||||||
|
|
||||||
HOST = "127.0.0.1" # Standard loopback interface address (localhost)
|
|
||||||
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
|
|
||||||
clients=0
|
|
||||||
connections=[]
|
|
||||||
|
|
||||||
def send_message(conn, data):
|
|
||||||
for i in connections:
|
|
||||||
if i != conn:
|
|
||||||
i.sock.send(data)
|
|
||||||
|
|
||||||
def handle_connection(conn):
|
|
||||||
with conn.sock:
|
|
||||||
while True:
|
|
||||||
data = conn.sock.recv(1024)
|
|
||||||
if data:
|
|
||||||
send_message(conn, data)
|
|
||||||
else:
|
|
||||||
print(f"Disconnected: {conn.addr}")
|
|
||||||
connections.remove(conn)
|
|
||||||
break
|
|
||||||
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
||||||
try:
|
|
||||||
s.bind((HOST, PORT))
|
|
||||||
while True:
|
|
||||||
conn=Connection()
|
|
||||||
s.listen()
|
|
||||||
conn.sock, conn.addr = s.accept()
|
|
||||||
connections.append(conn)
|
|
||||||
threading.Thread(target=handle_connection, args=(conn,)).start()
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
print("Shutdown server")
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user