Here's an example code to create a HTTP server in Python and reading the request body as multipart form data using the built-in http.server and cgi modules:
main.py933 chars30 linesThis code defines a custom request handler (MyRequestHandler) by extending http.server.BaseHTTPRequestHandler class. In the do_POST() method, it reads the request body as multipart form data using the cgi.parse_multipart() method and prints the fields. Finally, it sends a response with 200 OK status code.
The run_server() function starts the HTTP server on the given port (default is 8000) using HTTPServer class from http.server module. When the server receives a request, it calls MyRequestHandler to handle the request.
You can run this code from the command line by running python server.py. Then, you can send a POST request with multipart form data using a tool like curl:
main.py101 chars2 lines
gistlibby LogSnag