简体   繁体   中英

How to pass Queue to baseHTTPServer

I'm using the example bellow with Thread, a simple http server to listen to post request and collect the data for the main process. I want to convert it to use multiprocessing and passing a Queue in order to collect the POST data but I'm not sure how to pass it, any idea on how to do so?

from threading import Thread
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO

KEEP_RUNNING = True
COLLECT_POST_DATA = []

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        body = self.rfile.read(content_length)
        print("body: {}".format(body))
        COLLECT_POST_DATA.append(body)
        self.send_response(200)
        self.end_headers()
        response = BytesIO()
        response.write(b'This is POST request. ')
        response.write(b'Received: ')
        response.write(body)
        self.wfile.write(response.getvalue())

def run_server(port):
    httpd = HTTPServer(('0.0.0.0', port), SimpleHTTPRequestHandler)
    httpd.timeout = 1
    while KEEP_RUNNING:
        httpd.handle_request()
    # httpd.serve_forever()

t = Thread(target = run_server, args=(8000,))
t.start()

# to kill
KEEP_RUNNING = False

So I worked it out:

import multiprocessing
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO

class QueuingHTTPServer(HTTPServer):

    # def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True, queue=False):
    def __init__(self, server_address, RequestHandlerClass, queue, bind_and_activate=True):
        HTTPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate)
        self.queue = queue

class PostHTTPRequestHandler(BaseHTTPRequestHandler):

    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        body = self.rfile.read(content_length)
        print("body: {}".format(body))
        self.send_response(200)
        self.end_headers()
        response = BytesIO()
        response.write(b'This is POST request. ')
        response.write(b'Received: ')
        response.write(body)
        self.wfile.write(response.getvalue())
        self.server.queue.put(body)

def run_server(port, queue):
    print('run_server')
    httpd = QueuingHTTPServer(('0.0.0.0', port), PostHTTPRequestHandler, queue)
    httpd.timeout = 2
    while True:
        httpd.handle_request()
    # httpd.serve_forever()

queue = multiprocessing.Queue()
p = multiprocessing.Process(target=run_server, name='serve', args=(8000, queue))
p.start()

try it with:

curl hostip:8000 -m 5 -d"12.12.12.12"

Then from the script:

data = queue.get(timeout=2)

kill by:

p.terminate()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM