Why does a python program not interrupt with Ctlr+d or Ctrl+c?
<p>This is a client code from my server-client chat program but when i'm trying to close this program with sending a letter <em>q</em>, it simply doesn't work. It stucks in the terminal and waits for something.</p>

<p>What did i do wrong here?</p>

<pre><code>import socket
import threading
import time

receiving = False
lock = threading.Lock()

def receive(name, sock):
        receiving = True
        while receiving:
                try:
                        lock.acquire()
                        while True:
                                data, address = sock.recvfrom(1024)
                                print str(data)
                except:
                        pass
                finally:
                        lock.release()

host = 'localhost'
port = 0
server = ('localhost', 7787)

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
s.setblocking(0)

receiveThread = threading.Thread(target=receive, args=("receiveThread", s))
receiveThread.start()

username = raw_input("Name: ")
message = raw_input(username + "-&gt; ")

while True:
        if message == 'q':
                receiving = False
                receiveThread.join()
                s.close()
                break
        if message != '':
                s.sendto(username + ": " + message, server)
        lock.acquire()
        message = raw_input(username + "-&gt; ")
        lock.release()
        time.sleep(0.2)
</code></pre>

<p>In case you are interested in seeing the server code, here it is:</p>

<pre><code>import socket
import threading
import time

host = 'localhost'
port = 7787

running = False

MAX_PACKET_SIZE = 1024
clients = []

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
s.setblocking(0)


running = True
print "server started..."

while running:
        try:
                data, address = s.recvfrom(MAX_PACKET_SIZE)
                if 'quit' in str(data):
                        running = False
                        print 'server stopped...'
                if address not in clients:
                        clients.append(address)

                print time.ctime(time.time()) + str(address) + ": :" + str(data)
                for client in clients:
                        s.sendto(data, client)
        except:
                pass

running = False
s.close()
</code></pre>

<p>Because in python ctrl+C triggers KeyboardInterrupt exception to be thrown. This allows you to gracefully shut down your program. And you catch this exception with </p>

<pre><code>            except:
                    pass
</code></pre>

python