To create a HTTP server in Python, you can make use of the built-in http.server
module. Here's an example of how to create an HTTP server and read the request body as text in Python:
main.py601 chars20 lines
In this example, we define a RequestHandler
class that inherits from BaseHTTPRequestHandler
. We override the do_POST
method to capture POST requests and read the request body using the rfile
attribute. We then send a 200 response code with the send_response
method, and write the response body with the wfile
attribute.
Lastly, we define a run_server
function that creates an instance of our HTTP server and starts it with the serve_forever
method. When you run this script, the server will listen on port 8000 by default. When a POST request is received, the server will respond with the request body as text.
gistlibby LogSnag