The CTRL+C event can be caught in a separate process and sent back to another thread running in the main process to kill the socket. Example below, tested successfully on Windows 10 with Python 3.5.4. Placed some comments and print statements around so you can see what's happening.
from multiprocessing import Pipe, Process
from socket import socket, AF_INET, SOCK_STREAM
from threading import Thread
import time
def detect_interrupt(conn):
try:
print("Listening for KeyboardInterrupt...")
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Detected KeyboardInterrupt!")
print("Sending IPC...")
conn.send(True)
conn.close()
def listen_for_interrupt(conn, sock):
print("Listening for IPC...")
conn.recv()
print("Detected IPC!")
print("Closing sock...")
sock.close()
if __name__ == "__main__":
sock = socket(AF_INET, SOCK_STREAM)
sock.bind(("localhost", 7777))
sock.listen(1)
# Crate a Pipe for interprocess communication
main_conn, detect_conn = Pipe()
# Create a thread in main process to listen on connection
listen_for_interrupt_thread = Thread(
target=listen_for_interrupt, args=(main_conn, sock), daemon=True)
listen_for_interrupt_thread.start()
# Create a separate process to detect the KeyboardInterrupt
detect_interrupt_process = Process(
target=detect_interrupt, args=(detect_conn,))
detect_interrupt_process.start()
connection = None
try:
while True:
print("Running socket accept()")
connection, address = sock.accept()
print("connected from " + address)
received_message = sock.recv(300)
if not received_message:
break
connection.sendall(b"hello")
except KeyboardInterrupt:
print("Handling KeyboardInterrupt")
sock.close()
if connection:
connection.close()