fix server
This commit is contained in:
parent
878c444c9a
commit
9d8eb07570
23
server.py
23
server.py
@ -33,24 +33,33 @@ def handle_connection(conn):
|
||||
connections.remove(conn)
|
||||
break
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
def handle_new_connections():
|
||||
hostname = socket.gethostname()
|
||||
IPAddr = socket.gethostbyname(hostname)
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
try:
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
s.bind((HOST, PORT))
|
||||
print(f"Server {hostname} on ip {IPAddr}")
|
||||
while run:
|
||||
conn=Connection()
|
||||
s.listen()
|
||||
ready_to_read, _, _ = select.select([s], [], [], 1.0)
|
||||
if ready_to_read:
|
||||
conn.sock, conn.addr = s.accept()
|
||||
connections.append(conn)
|
||||
threading.Thread(target=handle_connection, args=(conn,)).start()
|
||||
except KeyboardInterrupt:
|
||||
run = False
|
||||
print("Shutdown server")
|
||||
finally:
|
||||
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")
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user