With python socketserver how can I pass a variable to the constructor of the handler class With python socketserver how can I pass a variable to the constructor of the handler class python python

With python socketserver how can I pass a variable to the constructor of the handler class


Unfortunately, there really isn't an easy way to access the handlers directly from outside the server.

You have two options to get the information to the EchoHandler instances:

  1. Store the connection as a property of the server (add server.conn = conn before calling server_forever()) and then access that property in EchoHandler.handler through self.server.conn.
  2. You can overwrite the server's finish_request and assign the value there (you would have to pass it to the constructor of EchoHandler and overwrite EchoHandler.__init__). That is a far messier solution and it pretty much requires you to store the connection on the server anyway.

My optionon of your best bet:

class EchoHandler(SocketServer.StreamRequestHandler):    def handle(self):        # I have no idea why you would print this but this is an example        print( self.server.conn );        print self.client_address, 'connected'if __name__ == '__main__':    SocketServer.ForkingTCPServer.allow_reuse_address = 1    server = SocketServer.ForkingTCPServer(('10.0.0.6', 4242), EchoHandler)    server.conn = MySQLdb.connect (host = "10.0.0.5",                      user = "user", passwd = "pass", db = "database")    # continue as normal


Mark T has the following to say on the python list archive

In the handler class, self.server refers to the server object, so subclassthe server and override init to take any additional server parametersand store them as instance variables.

import SocketServerclass MyServer(SocketServer.ThreadingTCPServer):    def __init__(self, server_address, RequestHandlerClass, arg1, arg2):        SocketServer.ThreadingTCPServer.__init__(self,                                                  server_address,                                                  RequestHandlerClass)        self.arg1 = arg1        self.arg2 = arg2class MyHandler(SocketServer.StreamRequestHandler):    def handle(self):        print self.server.arg1        print self.server.arg2


Another way, that I believe more pythonic, is to do the following:

class EchoHandler(SocketServer.StreamRequestHandler):  def __init__(self, a, b):    self.a = a    self.b = b  def __call__(self, request, client_address, server):    h = EchoHandler(self.a, self.b)    SocketServer.StreamRequestHandler.__init__(h, request, client_address, server)

You can now give an instance of your handler to the TCPServer:

SocketServer.ForkingTCPServer(('10.0.0.6', 4242), EchoHandler("aaa", "bbb"))

The TCPServer normally creates a new instance of EchoHandler per request but in this case, the __call__ method will be called instead of the constructor (it is already an instance.)

In the call method, I explicitly make a copy of the current EchoHandler and pass it to the super constructor to conform to the original logic of "one handler instance per request".

It is worth having a look at the SocketServer module to understand what happens here: https://github.com/python/cpython/blob/2.7/Lib/SocketServer.py