I created for learning purposes a mock-API project containing a simple db.py file as well as a server.py file for deployment using Python 3.11.4. The db.py file contains a small dictionary named mock_api. The server.py contains the following script:


from http.server import BaseHTTPRequestHandler, HTTPServer
import json
from db import mock_api
import os

class MyServer(BaseHTTPRequestHandler):

    def do_GET(self):
        if self.path == "/mock_api/books":
            self.send_response(200)
            self.send_header("Content-type", "application/json")
            self.end_headers()

            response = json.dumps(mock_api).encode("utf-8")

            self.wfile.write(response)
 
if __name__ == "__main__":
    serverPort = int(os.environ.get("PORT", 3000)) 
    webServer = HTTPServer(("", serverPort), MyServer)
    print(f"Starting server on port {serverPort}")
    webServer.serve_forever()

I tried to deploy my project via https://render.com, and therefore expected my mock api to be accessible via the URL https://mock-api-books-1qz9.onrender.com and rendered on a webpage. However, when deploying my project, the terminal returns the following error message: "TypeError: BaseRequestHandler.init() missing 1 required positional argument: 'server'"(see snippet image). And when I access the URL https://mock-api-books-1qz9.onrender.com, the error message 'Internal Server Error' is displayed.

I understand from the terminal error message that there is a positional argument for the parameter 'server' missing within the init() statement of BaseRequestHandler. Looking 'under the hood' BaseRequestHandler appears to be a parent class of BaseHTTPRequestHandler, the class that MyServer inherits from. I don't understand, what for argument is expected for the parameter 'server' and where I need to pass it in within my server.py script. Thank you in advance.

0

There are 0 best solutions below