Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > passing arguments to tcpserver classes

Reply
Thread Tools

passing arguments to tcpserver classes

 
 
Eric Spaulding
Guest
Posts: n/a
 
      06-13-2007
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.

 
Reply With Quote
 
 
 
 
Justin Ezequiel
Guest
Posts: n/a
 
      06-14-2007
On Jun 13, 10:19 pm, Eric Spaulding <(E-Mail Removed)> 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.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)

 
Reply With Quote
 
 
 
 
Mark T
Guest
Posts: n/a
 
      06-14-2007

"Eric Spaulding" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
functions and arguments.length; passing unknown number of arguments oldyork90 Javascript 10 09-27-2008 03:05 AM
Question About TCPServer & TCPSocket classes Steve Lewis Ruby 4 09-07-2008 01:02 AM
Passing Classes as Arguments to Functions of Other Classes crouse@physics.wm.edu Java 7 08-12-2006 02:08 AM
passing custom classes or stucture as arguments Joe Abou Jaoude ASP .Net Web Services 4 12-14-2005 09:43 AM
Passing arguments to Junit classes from ant? Roy Smith Java 1 04-30-2004 09:38 PM



Advertisments