TCP server

Pedro Werneck pedro.werneck at terra.com.br
Mon Jan 24 10:04:11 EST 2005


A quick example for you:

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

###

Telnet to localhost 5000 and any data you sent will be echoed back to you... send 'bye' and the connection is closed




On 24 Jan 2005 06:25:18 -0800
"assaf" <assafs at ixi.com> wrote:

> i am try to create a server
> what am i suppose to send to SocketServer.TCPServer
> what is the client_address ("127.0.0.1:80" ?)
> and BaseRequestHandler = ?
> 
> thanks
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list