roschach
roschach

Reputation: 9336

How to pass parameter to GET-handler method of BaseHTTPRequestHandler class in Python

I have a HTTP server in Python created with

    from http.server import BaseHTTPRequestHandler, HTTPServer
    import bson.json_util
    import json
    server = HTTPServer((ip, port), GetHandler)
    print ('Starting server, use <Ctrl-C> to stop')
    server.serve_forever()

where GetHandler

class GetHandler(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()

    def do_GET(self):
        json_string = json.dumps({"message" : "Hello"}, default=bson.json_util.default)+"\n"
        self._set_headers()
        self.wfile.write(json_string.encode())#encoding='UTF-8'
        return

Suppose I want to pass a variable to the method do_GET of the class GetHandler from the line server = HTTPServer((ip, port), GetHandler): how can I do that?

EDIT

Just as example suppose that I have a string my_str defined before the line server = HTTPServer((ip, port), GetHandler) and I want to use this string as response message.

Upvotes: 3

Views: 2866

Answers (1)

exhuma
exhuma

Reputation: 21687

In Python you can actually create classes inside functions. These classes will then have access the the function's locals via a closure.

The following code defines the function make_handler which, when called defines a new "handler" class with the value in myvariable available inside the whole class:

import json
from http.server import BaseHTTPRequestHandler, HTTPServer


def make_handler(myvariable):

    class GetHandler(BaseHTTPRequestHandler):
        def _set_headers(self):
            self.send_response(200)
            self.send_header('Content-type', 'text/plain')
            self.end_headers()

        def do_GET(self):
            self._set_headers()
            self.wfile.write(myvariable.encode('utf8'))
            return

    return GetHandler

server = HTTPServer(('127.0.0.1', 50002), make_handler('my_str'))
print ('Starting server, use <Ctrl-C> to stop')
server.serve_forever()

Upvotes: 5

Related Questions