如何将队列放入Python 2 BaseHTTPRequestHandler?

时间:2015-02-03 20:42:03

标签: python basehttpserver basehttprequesthandler

使用Python 2.7,我扩展了BaseHTTPServer.BaseHTTPRequestHandler以支持do_POST方法。我想给请求处理程序一个队列,以便它可以将发布的数据放在队列上以供另一个线程处理。

这是我班级的精简版:

import BaseHTTPServer
import json

class PostHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    def do_POST(self):
        self.send_response(200)
        self.end_headers()

        length = int(self.headers['Content-Length'])
        self.post_data = self.rfile.read(length)

        try:
            if self.headers['Content-Type'] == 'application/json':
                self.post_data = json.loads(self.post_data)
                self.log_message(json.dumps(self.post_data))

                ### WANT TO PUT self.post_data ON A QUEUE HERE ###

        except KeyError as error:
            self.log_message('No Content-Type header')
        except ValueError as error:
            self.log_message("%s" % error)

从那以后处理程序由BaseHTTPServer创建,我不认为我可以改变 init 方法来传入队列。

我希望我的main()看起来像这样:

def main():

    import logging
    import Queue
    import signal
    import threading
    import traceback


    try:
        # set stoprequest to accept Ctrl+c
        stoprequest = threading.Event()
        signal.signal(signal.SIGINT, lambda signal, frame: stoprequest.set())

        args = _get_main_args()

        ### HERE IS THE QUEUE, HOW TO I GIVE A REFERENCE TO THE HANDLER??? ###
        data_queue = Queue.Queue()

        handler = PostHTTPRequestHandler

        server = BaseHTTPServer.HTTPServer((args.address, args.port), handler)

        server_thread = threading.Thread(target=server.serve_forever)
        server_thread.daemon = True
        server_thread.start()

        while not stoprequest.is_set():

            try:
                data = data_queue.get(False)

                ### I WANT TO PROCESS THE DATA HERE ###

            except Queue.Empty:
                pass

        server.shutdown()

        #logging.debug("Exiting with return code 0")
        return 0

    except Exception:
        sys.stderr.write("%s" % traceback.format_exc())
        return 1

2 个答案:

答案 0 :(得分:0)

我现在看到BaseRequestHandler的init看起来像这样:

class BaseRequestHandler:

    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()

所以我将扩展BaseHTTPServer以包含一个队列,然后它将可供处理程序使用。

class QueuingHTTPServer(BaseHTTPServer.HTTPServer):

    def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True):
        BaseHTTPServer.HTTPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate)
        self.data_queue = Queue.Queue()

所以现在处理程序看起来像这样:

class PostHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    def do_POST(self):
        self.send_response(200)
        self.end_headers()

        length = int(self.headers['Content-Length'])
        self.post_data = self.rfile.read(length)

        try:
            if self.headers['Content-Type'] == 'application/json':
                self.post_data = json.loads(self.post_data)
                self.log_message(json.dumps(self.post_data))

                try:
                    self.server.data_queue.put(self.post_data)
                except Queue.Full:
                    pass

        except KeyError as error:
            self.log_message('No Content-Type header')
        except ValueError as error:
            self.log_message("%s" % error)

答案 1 :(得分:0)

我修改了Josh提交的代码,以使用共享的Queue引用,而不是在实例化期间创建一个。

class QueuingHTTPServer(BaseHTTPServer.HTTPServer):

    def __init__(self, server_address, RequestHandlerClass, data_queue, bind_and_activate=True):
        BaseHTTPServer.HTTPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate)
        self.data_queue = queue