stuck in while loop python

Posted by user1717330 on Stack Overflow See other posts from Stack Overflow or by user1717330
Published on 2012-11-06T16:58:03Z Indexed on 2012/11/06 16:59 UTC
Read the original article Hit count: 207

Filed under:
|

I am creating a chat server in python and got quite far as a noob in the language. I am having 1 problem at the moment which I want to solve before I go further, but I cannot seem to find how to get the problem solved.

It is about a while loop that continues..

in the below code is where it goes wrong

while 1:
    try:
        data = self.channel.recv ( 1024 )
        print "Message from client: ", data
        if "exit" in data:
            self.channel.send("You have closed youre connection.\n")
            break
    except KeyboardInterrupt:
        break
    except:
        raise

When this piece of code get executed, on my client I need to enter "exit" to quit the connection. This works as a charm, but when I use CTRL+C to exit the connection, my server prints "Message from client: " a couple of thousand times.

where am I going wrong?

© Stack Overflow or respective owner

Related posts about python

Related posts about while-loop