passing arguments to tcpserver classes

Discussion in 'Python' started by Eric Spaulding, Jun 13, 2007.

  1. 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.

    Where TCPHandlerClass:

    class TCPHandlerClass(SocketServer.StreamRequestHandler):
    def handle(self):
    #handle stream events here#


    Thanks for any advice.
     
    Eric Spaulding, Jun 13, 2007
    #1
    1. Advertising

  2. On Jun 13, 10:19 pm, Eric Spaulding <> 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)
     
    Justin Ezequiel, Jun 14, 2007
    #2
    1. Advertising

  3. Eric Spaulding

    Mark T Guest

    "Eric Spaulding" <> wrote in message
    news:...
    > 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.
    >
    > Where TCPHandlerClass:
    >
    > class TCPHandlerClass(SocketServer.StreamRequestHandler):
    > def handle(self):
    > #handle stream events here#
    >
    >
    > Thanks for any advice.
    >


    In the handler class, self.server refers to the server object, so subclass
    the server and override __init__ to take any additional server parameters
    and store them as instance variables.

    import SocketServer

    class MyServer(SocketServer.ThreadingTCPServer):
    def __init__(self, server_address, RequestHandlerClass,arg1,arg2):
    SocketServer.ThreadingTCPServer.__init__(self,server_address,RequestHandlerClass)
    self.arg1 = arg1
    self.arg2 = arg2

    class MyHandler(SocketServer.StreamRequestHandler):
    def handle(self):
    print self.server.arg1
    print self.server.arg2

    if __name__ == '__main__':
    srv = MyServer(('',5000),MyHandler,123,456)
    srv.serve_forever()

    --Mark
     
    Mark T, Jun 14, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. huy
    Replies:
    3
    Views:
    513
    Dennis Lee Bieber
    Aug 17, 2004
  2. dcrespo

    Using SRP on TCPServer module

    dcrespo, Oct 14, 2005, in forum: Python
    Replies:
    1
    Views:
    322
    Paul Rubin
    Oct 14, 2005
  3. Replies:
    7
    Views:
    578
    Alex Hunsley
    Aug 12, 2006
  4. Steve Lewis
    Replies:
    4
    Views:
    128
    Steve Lewis
    Sep 7, 2008
  5. oldyork90
    Replies:
    10
    Views:
    360
    Jorge
    Sep 27, 2008
Loading...

Share This Page