passing arguments to tcpserver classes

Justin Ezequiel justin.mailinglists at gmail.com
Thu Jun 14 00:44:53 EDT 2007


On Jun 13, 10:19 pm, Eric Spaulding <e... at cornell.edu> wrote:
> Is there an easy way to pass arguments to a handler class that is used
> by the standard TCPServer?
>
> normally --> srvr =SocketServer.TCPServer(('',port_num), TCPHandlerClass)
>
> I'd like to be able to: srvr =SocketServer.TCPServer(('',port_num),
> TCPHandlerClass, (arg1,arg2))
>
> And have arg1, arg2 available via TCPHandlerClass.__init__ or some other
> way.
>

I use the following method.
Would also like to know if there's another way to do this.

class SVNUpdateRequestHandler(SocketServer.BaseRequestHandler):
    def __init__(self, svn, wms, *args, **kwargs):
        self.svn = svn
        self.wms = wms
        # NEED to set additional attributes before parent init
        SocketServer.BaseRequestHandler.__init__(self, *args,
**kwargs)

    def handle(self):
        pass

def get_handler(svn, wms):
    class RequestHandler(SVNUpdateRequestHandler):
        def __init__(self, *args, **kwargs):
            SVNUpdateRequestHandler.__init__(self, svn, wms,
                                             *args, **kwargs)

    return RequestHandler

def main(port, requesthandler):
    server = SVNUpdateServer(('', port), requesthandler)
    while 1:
        server.handle_request()

if __name__ == '__main__':
    svn, wms = sys.argv[1:]

    requesthandler = get_handler(svn, wms)
    main(port, requesthandler)




More information about the Python-list mailing list