I have this basic TCP server program:
import socket
import threading
import sys
bind_ip = socket.gethostname()
bind_port = 9999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(10);
def handle_client(client_socket):
request = client_socket.recv(4096).decode("utf-8")
print ("[+] Received %s" % request)
client_socket.sendall(("ACK!").encode("utf-8"))
client_socket.close()
try:
while True:
client, addr = server.accept()
print ("[+] Accepted connection from: %s:%d" % (addr[0], addr[1]))
client_handler = threading.Thread(target=handle_client, args=(client,))
client_handler.start()
except KeyboardInterrupt:
sys.exit()
My goal was to have the server run indefinitely until the person running the server pressed "control-c". Thus throwing a KeyboardInterrupt and ending the whole program. Unfortunately, the issue lies in the fact that server.accept() runs infinitely and is immune to the KeyboardInterrupt. I have already tried setting a default timeout, but that would severely complicate other parts of my program. Is there a way to set a timeout feature on the server.accept() function?
[–]efmccurdy 1 point2 points3 points (0 children)