0

I'm trying to set a custom object as an attribute to a TCP handler class documented here. In my case both the server and client handlers need a custom object:

import SocketServer

class MyTCPHandler(SocketServer.BaseRequestHandler):
    """
    The request handler class for our server.

    It is instantiated once per connection to the server, and must
    override the handle() method to implement communication to the
    client.
    """

    def set_custom_object(self, custom_object):
        self.customObject = custom_object

    def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        # Call a custom method of the custom object over the data
        self.data = self.customObject.custom_method(self.data)
        # Send back the processed data 
        self.request.sendall(self.data)

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999

    # Create the server, binding to localhost on port 9999
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)

    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()

How may I bind the customObject to the handler class?

The BaseRequestHandler offers a set of overridable methods but can't find where they're invoked.

class BaseRequestHandler:

    """Base class for request handler classes.

    This class is instantiated for each request to be handled.  The
    constructor sets the instance variables request, client_address
    and server, and then calls the handle() method.  To implement a
    specific service, all you need to do is to derive a class which
    defines a handle() method.

    The handle() method can find the request as self.request, the
    client address as self.client_address, and the server (in case it
    needs access to per-server information) as self.server.  Since a
    separate instance is created for each request, the handle() method
    can define arbitrary other instance variariables.

    """

    def __init__(self, request, client_address, server):
        self.request = request
        self.client_address = client_address
        self.server = server
        self.setup()
        try:
            self.handle()
        finally:
            self.finish()

    def setup(self):
        pass

    def handle(self):
        pass

    def finish(self):
        pass
Sebi
  • 4,262
  • 13
  • 60
  • 116
  • How doesn't what you already have work? – Ignacio Vazquez-Abrams Jun 18 '16 at 09:27
  • I do not have an instance of "MyTCPHandler" so I'm unable to call set_custom_object(). SocketServer.TCPServer() binds the handler class as a whole. – Sebi Jun 18 '16 at 09:30
  • what does the "customObject" do? "Binding" is a very broad concept. – gdlmx Jun 18 '16 at 09:48
  • customObject contains a number of encoders I have to apply over the raw data as received from the client. By binding, I mean assigning an instance of customObject to an attribute from the handler class so that it may later be used to decode data. – Sebi Jun 18 '16 at 09:51
  • If all instances of `MyTCPHandler` can share the same custom object (encoders in your case), it'll be sufficient to store them as static properties in the class, or to define a [classmethod](http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod-for-beginner) – gdlmx Jun 18 '16 at 09:53
  • Yes, they all share the same instance of the custom object. The custom object has state variables that may be altered by requests. Would it be better to store it globally and use a mutex for each request? – Sebi Jun 18 '16 at 09:59
  • 1
    If those state variables need to be shared among different requests, I would do that. Otherwise I would create them dynamically inside `handle()`. Both storing in global namespace and class namespace will work. It depends whether you want to encapsulate them. – gdlmx Jun 18 '16 at 10:09

0 Answers0