a simple tcp server sample

Tzury Bar Yochay Afro.Systems at gmail.com
Thu Nov 8 06:42:03 CET 2007


here is its:
# a simple tcp server

import SocketServer


class EchoRequestHandler(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)
            self.request.send(data)
            if data.strip() == 'bye':
                return

    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),
EchoRequestHandler)
server.serve_forever()




More information about the Python-list mailing list