Compare commits

..

3 Commits

Author SHA1 Message Date
MuedeHydra
9d8eb07570 fix server 2025-11-24 19:11:32 +01:00
MuedeHydra
878c444c9a Merge remote-tracking branch 'refs/remotes/origin/main' 2025-11-24 18:58:39 +01:00
MuedeHydra
ada3404835 add ninally 2025-11-24 18:57:44 +01:00
2 changed files with 24 additions and 15 deletions

View File

@ -60,9 +60,9 @@ if __name__ == "__main__":
read_msg(s) read_msg(s)
except ConnectionRefusedError: except ConnectionRefusedError:
print("Could not connect to the server!") print("Could not connect to the server!")
finally:
s.close()
except KeyboardInterrupt: except KeyboardInterrupt:
print("Exit with CTRL+C") print("Exit with CTRL+C")
finally:
pass

View File

@ -33,24 +33,33 @@ def handle_connection(conn):
connections.remove(conn) connections.remove(conn)
break break
def handle_new_connections():
if __name__ == "__main__":
hostname = socket.gethostname() hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname) IPAddr = socket.gethostbyname(hostname)
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind((HOST, PORT)) s.bind((HOST, PORT))
print(f"Server {hostname} on ip {IPAddr}") print(f"Server {hostname} on ip {IPAddr}")
while run: while run:
conn=Connection() conn=Connection()
s.listen() s.listen()
conn.sock, conn.addr = s.accept() ready_to_read, _, _ = select.select([s], [], [], 1.0)
connections.append(conn) if ready_to_read:
threading.Thread(target=handle_connection, args=(conn,)).start() conn.sock, conn.addr = s.accept()
except KeyboardInterrupt: connections.append(conn)
run = False threading.Thread(target=handle_connection, args=(conn,)).start()
print("Shutdown server") finally:
finally: s.close()
s.close()
if __name__ == "__main__":
threading.Thread(target=handle_new_connections).start()
try:
print("Press Enter or CTRL + C to stop server")
input()
except KeyboardInterrupt:
pass
finally:
run = False
print("Shutdown server")