When i run my server, when server is listening, there is no way to stop the server rather than closing the terminal. I tried to handle ctrl+c using KeyboardInterrupt, but it doesnt work. I can terminate the script using ctrl+break but i dont know how to handle it in my code.
Here is the code for my server:
import socket
try:
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind(('127.0.0.1', 4444))
listener.listen(0)
print('Listening...')
listener.accept()
print('Got a connection.')
except KeyboardInterrupt:
print('Exiting...')
exit()
How can i handle ctrl+break in my code?