Here is a simple example on how to create an HTTP server in Python using the built-in http.server
module, and parse a form-urlencoded request body:
main.py840 chars24 lines
This code defines a RequestHandler
class that extends BaseHTTPRequestHandler
, and overrides the do_POST
method to extract the form-urlencoded request body from the HTTP request, and parse it into a dictionary using parse_qs
from the urllib.parse
module.
The parsed parameters are then sent back as a plain text response with keys and values separated by equals signs.
To start the server, we create an instance of HTTPServer
and pass it the server address and request handler. Finally, we call serve_forever
on the server instance to start accepting incoming requests.
gistlibby LogSnag