如何让 HTTPServer 将我自己的 class 与 do_GET 一起传回?

How to get HTTPServer to pass my own class back with do_GET's?

我有一个特殊的数据库 class 实例,我想将其传递给 HTTPServer 处理程序,以便我可以将其返回以用于我的 do_GET 和 do_POST 回调处理程序。我尝试 subclassing 将我的数据库 class 添加为 Handler 的附加参数...但是,这并没有完全进入我的 HTTPServerRequestHandler class.

我也尝试过但没有用:

即使我设法将参数添加到 HTTPServerRequestHandler class,这只会产生错误,因为 serve_forever 仍将使用原始 4 ( self+3) 参数,省略我的第 5 个(数据库)参数。

以前,我会将数据库 class 实例化为全局常量,但这似乎是个坏主意。

这是我目前所做的工作:

此代码成功地为我的页面提供服务,但我无法访问任何数据库方法(包括我的日志处理程序,它是数据库实例的一部分):

def run_server(state_database, port):
    state_database.log.info('starting server.')
    server_address = ('', port)

    HandlerClass = MakeHandlerClassforDB(state_database)

    httpserver = HTTPServer(server_address, HandlerClass)
    state_database.log.info('Server loaded.')
    httpserver.serve_forever()


def MakeHandlerClassforDB(state_database):
    class CustomHandler(HTTPServerRequestHandler, object):
        def __init__(self, *args, **kwargs):
            self.database = state_database
            super(CustomHandler, self).__init__(*args, **kwargs)
    return CustomHandler


# HTTPRequestHandler class
# noinspection PyPep8Naming
class HTTPServerRequestHandler(BaseHTTPRequestHandler):
    def __init__(self, request, client_address, server):
        BaseHTTPRequestHandler.__init__(self, request, client_address, server)
        # would like to be able to get database into here as a self....
    def do_GET(self):
        ip = self.client_address[0].split(".")
        if ip[0] in self.page.blocked_ip_highs:
            # `self.database` does not resolve, so this does not work...
            self.database.log.info(
                "Ignored request from %s" % self.client_address)
            return
          ...

    def do_POST(self):
        time_start = time.time()
        # again, self.database does not exist in the callback
        self.database.log.debug(
            "Headers_________\n%s\n______________\n" % self.headers)

我也希望它在 Python 3 和 Python 2 上都有效(这就是为什么我不使用 super().__init__(...)。)

这似乎有效:

def run_server(state_database, port):
    server_address = ('', port)

    handlerclass = makehandlerclassfordb(state_database)
    httpserver = HTTPServer(server_address, handlerclass)

    httpserver.serve_forever()


def makehandlerclassfordb(state_database):
    class CustomHandler(HTTPServerRequestHandler, object):
        def __init__(self, *args, **kwargs):
            self.database = state_database
            self.page = HTMLPage()

            super(CustomHandler, self).__init__(*args, **kwargs)
    return CustomHandler


class HTTPServerRequestHandler(BaseHTTPRequestHandler):

     def do_GET(self):
        self.database.log.debug(
            "Get Header_________%s______________" % self.headers)

        """both self.page and self.database DO resolve (although
            PyCharm does not think they should :P )"""

        ip = self.client_address[0].split(".")
        if ip[0] in self.page.blocked_ip_highs:
            self.database.log.info(
                "Ignored request from %s" % self.client_address)
            return

我可以 re-add 将项目 class HTTPServerRequestHandler __init__,但这不会改变功能,我认为没有理由只做一个 IDE错误消失。