To create an HTTP server with a specific route and a specific status code in Python, you can use the built-in "http.server" module. Here's an example:
main.py532 chars17 lines
In this example, we define a custom request handler class MyHandler
that inherits from BaseHTTPRequestHandler
. In the do_GET
method, we check if the requested path is '/my/route'. If it is, we send a 200 status code with a simple HTML response, otherwise we send a 404 error.
We then create an instance of HTTPServer
with our handler on port 8000 and start the server. When the server is running, you can visit http://localhost:8000/my/route
in your web browser to see the "Hello, World!" message.
gistlibby LogSnag