100% CPU Usage when a tcp client is disconnected

Tzury Bar Yochay Afro.Systems at gmail.com
Thu Nov 22 15:53:37 CET 2007

The following is a code I am using for a simple tcp echo server.
When I run it and then connect to it (with Telnet for example) if I
shout down the telnet the CPU tops 100% of usage and saty there
Can one tell what am I doing wrong?


import SocketServer

class MyServer(SocketServer.BaseRequestHandler ):
    def setup(self):
        print self.client_address, 'connected!'
        self.request.send('hi ' + str(self.client_address) + '\n')

    def handle(self):
        while 1:
            data = self.request.recv(1024)
            if data.strip() == 'bye':

    def finish(self):
        print self.client_address, 'disconnected!'
        self.request.send('bye ' + str(self.client_address) + '\n')

    #server host is a tuple ('host', port)
server = SocketServer.ThreadingTCPServer(('', 50008), MyServer)

More information about the Python-list mailing list