create a http server and read the request body as html in python

To create an HTTP server in Python, you can make use of the built-in http.server module. Here's an example of how to create a simple server that listens on port 8000:

main.py
import http.server
import socketserver

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()
221 chars
11 lines

To read the request body as HTML, you'll need to access the stdin attribute of the request object. Here's an example code snippet that shows how to read the request body as HTML:

main.py
from http.server import BaseHTTPRequestHandler

class MyHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        body = self.rfile.read(content_length).decode('utf-8')
        print('Received HTML body:')
        print(body)
293 chars
9 lines

This handler assumes that the request is a POST request, and reads the request body from the rfile attribute of the request object. It then decodes the body as UTF-8 and prints it to the console. You can modify this code to suit your specific needs.

gistlibby LogSnag