BaseRequestHandler

Robin Munn rmunn at pobox.com
Fri Dec 27 12:41:29 EST 2002


Giorgi Lekishvili <gleki at gol.ge> wrote:
> Hi all, again!
> 
> I shall be much obliged if somebody sends me an example of how to
> redefine BaseRequestHandler class and its handle() method. Of course, I
> have to work myself, but more practical code would help me greatly. All
> I want right now is to learn Python network programming during the
> hollidays:)

Try the following (a simple "echo" server, copied straight from page 333
of _The Python Cookbook_):


    import SocketServer

    class MyHandler(SocketServer.BaseRequestHandler):
        def handle(self):
            while 1:
                dataReceived = self.request.recv(1024)
                if not dataReceived: break
                self.request.send(dataReceived)

    myServer = SocketServer.TCPServer(('',8881), MyHandler)
    myServer.serve_forever()


Or a more sophisticated example, showing how to use a dispatch method
(this one is my own code):


    import SocketServer

    class CmdHandler(SocketServer.BaseRequestHandler):
        """Handle commands coming in over a socket.

        Command format is as follows:
            command [args]

        Commands are case-insensitive: "get" == "Get" == "GET"

        Commands are separated by newlines, and the first word on each
        line is assumed to be the "verb" of the command, defining the
        action. The rest of the words are passed as parameters. Words
        are delimited by whitespace.

        BUGS: There is currently no way of including whitespace in a
        parameter. Adding this feature is left as an exercise to the
        reader.
        """

        def handle(self):
            commands = self.request.readlines()
            for command in commands:
                words = command.split()
                verb, params = words[0], words[1:]
                verb_handler = 'do_' + verb.upper()
                if hasattr(self, verb_handler):
                    return verb_handler(*params)

        def do_ECHO(self, *params):
            for item in params:
                self.request.send(str(item) + ' ')
            self.request.send('\r\n')

        def do_HAIKU(self):
            self.request.send('Error 404 - a haiku\r\n')
            self.request.send('\r\n')
            self.request.send('You step in the stream,\r\n')
            self.request.send('  But the water has moved on.\r\n')
            self.request.send('This page is not here.\r\n')

        def do_SING(self, *params):
            songname = (' '.join(params)).lower()
            if songname == 'the lumberjack song':
                self.request.send('Oh, I'm a lumberjack and I'm OK,\r\n')
                self.request.send('I sleep all night and I work all day.\r\n')
            elif songname == 'the national anthem':
                # I'm American; change as appropriate for your country!
                self.request.send('Oh say can you see...\r\n')
            elif songname == 'silent night':
                self.request.send('Silent night, holy night,\r\n')
                self.request.send('All is calm, all is bright,\r\n')


    cmdServer = SocketServer.TCPServer(('',8882), CmdHandler)
    cmdServer.serve_forever()



I hope this gives you a few ideas to work with.

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838



More information about the Python-list mailing list