471,325 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

passing arguments to tcpserver classes

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.

Jun 13 '07 #1
2 6255
On Jun 13, 10:19 pm, Eric Spaulding <e...@cornell.eduwrote:
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.BaseRequestHa ndler):
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)

Jun 14 '07 #2

"Eric Spaulding" <el***@cornell.eduwrote in message
news:ma***************************************@pyt hon.org...
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,serv er_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

Jun 14 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Martin Lucas-Smith | last post: by
3 posts views Thread by Mantorok | last post: by
4 posts views Thread by Scott M. | last post: by
reply views Thread by Gonçalo Rodrigues | last post: by
1 post views Thread by 418928 | last post: by
4 posts views Thread by phoolpreet | last post: by
6 posts views Thread by =?iso-8859-1?q?Luis_M._Gonz=E1lez?= | last post: by
1 post views Thread by Jean Paul Mertens | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.