Created
March 11, 2021 12:28
-
-
Save in03/7ad0348bded554daf583a7ecd7b495dd to your computer and use it in GitHub Desktop.
socket server using multithreading to catch keyboard interrupts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Thanks GratefulGuest from SO: | |
# https://stackoverflow.com/questions/34871191/cant-close-socket-on-keyboardinterrupt | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment