ThreadingTCPServer

Steve Holden sholden at holdenweb.com
Fri Jan 25 12:55:35 EST 2002


"Hugo Martires" <hugomartires at hotmail.com> wrote ...
> How can i make a ThreadingTCPServer using the SocketServer module ?
> I have read all the information in the module and i still don't get it.
> I also learn how to program threads, but how to use them with SocketServer
?
> To start, i was doing this:
>
> #!/usr/bin/env python2
>
> from SocketServer import *
> import threading
>
> HOST=''
> PORT=21567
> ADDR=(HOST,PORT)
> BUFSIZE=1024
>
> def main():
>   print "Waiting..."
>   s = ThreadingTCPServer(ThreadingMixIn, TCPServer)
>
> what should i do ???
>
First, try not to use "from x import *" unless you know it to be safe for
module x.

HOST=''
PORT=21567
ADDR=(HOST,PORT)
import SocketServer

class MyRequestHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        print "Got a request"

myServer = SocketServer.ThreadingTCPServer(ADDR,
                MyRequestHandler)
myServer.serve_forever()

After that it's all down to your MyRequestHandler class, which will be used
to create an instance every time a client connects. Usually you put your
code in the handle() method, inheriting from SocketServer.BaseRequestHandler
to get the other details right.

regards
 Steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Python Web Programming: http://pydish.holdenweb.com/pwp/








More information about the Python-list mailing list