fix server
This commit is contained in:
parent
878c444c9a
commit
9d8eb07570
35
server.py
35
server.py
@ -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")
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user