Asperix
Asperix

Reputation: 3

How to stop python from closing connection on keyboardinterrupt?

So, im trying to make a little program which shows message boxes with messages i send it. and it works, but when i clientside close the connection the server script exits and i get this message:

Traceback (most recent call last):   File "server.py", line 9, in <module>
    conn, addr = s.accept()   File "C:\Python27\lib\socket.py", line 206, in accept
    sock, addr = self._sock.accept() KeyboardInterrupt

this is my python code:

import socket
from appJar import gui

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 1337              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()

while 1:
    data = conn.recv(1024)
    if not data: break
    app=gui("Besked fra Romeo")
    app.addLabel("idk",data)
    app.go()

how do i stop the server script from closing when i close my clientside script?

Upvotes: 0

Views: 654

Answers (1)

Peter
Peter

Reputation: 333

KeyboardInterrupt is just an exception. You should be able to catch it:

try:
    # Your loop
except KeyboardInterrupt:
    # Code that gets executed when ctrl-c is pressed

Upvotes: 2

Related Questions