473,388 Members | 1,322 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 6534
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Martin Lucas-Smith | last post by:
Having re-read www.php.net/functions.arguments recently, the notion of passing arguments by reference to a function makes a lot more sense now. However, my question is: is there any difference in...
20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
3
by: Mantorok | last post by:
Hi all I'm pretty new to ASP.Net, is it possible to pass arguments (maybe objects too) to web forms without using a querystring in the URL? Thanks Kev
4
by: Scott M. | last post by:
Does C# support passing arguments ByVal & ByRef? If so, what is the default (ByVal?)? And, if so, how do you explicitly specify either? Thanks.
0
by: Gonçalo Rodrigues | last post by:
Hi all, I have a single-rooted hierarchy of heap-allocated objects -- call the root Object. These objects are handled via a smart pointer template Reference<T>. Basically, Reference<Tis a...
1
by: 418928 | last post by:
Hi everybody, I understand that the standard way of passing arguments to a web page using javascript is "<url>?<arguments separated with "&" and values with "=">. Then, I can use...
4
by: phoolpreet | last post by:
hi guys i m stuck with the problem of passing arguments to main. we can define argc and argv and put statements in main that use these arguments. but how to set these value while running a...
6
by: =?iso-8859-1?q?Luis_M._Gonz=E1lez?= | last post by:
Please forgive me if what I'm asking is non sense... I created a little program to authomate the creation of the "setup.py" script for py2exe. It simply prompts for the main executable script...
1
by: Jean Paul Mertens | last post by:
Hello, Is there a simple way for passing arguments to applications deployed with ClickOnce and having off-line capabilities. My app takes arguments if I start it with the whole path to it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.