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?
CodePudding user response:
If no client is waiting, socket.accept()
hangs until a connection is made and can't be interrupted by CTRL-C or CTRL-BREAK. Use select.select()
to wait for a connection to be readable from the server socket along with a timeout. The timeout will allow CTRL-C and CTRL-BREAK to be recognized.
CTRL-C raises KeyboardInterrupt
, but CTRL-BREAK needs a signal handler. I've set up a custom exception to be raised on the SIGBREAK
signal below:
import signal
import socket
import select
# Custom exception with same base class as KeyboardInterrupt
class CtrlBreakInterrupt(BaseException):
pass
def handler(*args):
raise CtrlBreakInterrupt
signal.signal(signal.SIGBREAK, handler)
# Set up server
with socket.socket() as sock:
sock.bind(('', 5000))
sock.listen()
# Loop, accepting one connection at a time.
while True:
try:
# Check server has a connection with timeout
readable, _, _ = select.select([sock], [], [], 1.0)
if sock in readable:
with sock.accept() as (client, address):
while True:
data = client.recv(4096)
if not data: break
print(data)
except CtrlBreakInterrupt:
print('CTRL-BREAK')
break
except KeyboardInterrupt:
print('CTRL-C')
break
CodePudding user response:
something like this, perhaps.
def handler(signum, frame):
print 'Shutting down...'
sys.exit(1)
signal.signal(signal.SIGTERM, handler)
while True:
clientsocket, address = listener.accept()
clientsocket.send(bytes("hi.","utf-8"))