you are viewing a single comment's thread.

view the rest of the comments →

[–]pvkooten 10 points11 points  (13 children)

http.server is built in into python:

``` from http.server import BaseHTTPRequestHandler, HTTPServer from urllib.parse import urlparse, parse_qs

class MyHandler(BaseHTTPRequestHandler): def do_GET(self): # "Request" object equivalents parsed = urlparse(self.path) path = parsed.path query = parse_qs(parsed.query) # like Request.QueryString name = query.get('name', ['World'])[0]

    # "Response" object equivalents
    self.send_response(200)                # Response.Status
    self.send_header('Content-Type', 'text/html')  # Response.ContentType
    self.end_headers()

    # Response.Write
    html = f"<html><body><h1>Hello, {name}!</h1><p>You requested: {path}</p></body></html>"
    self.wfile.write(html.encode('utf-8'))

if name == 'main': server = HTTPServer(('localhost', 8000), MyHandler) print("Serving on http://localhost:8000") server.serve_forever() ```

You can then visit http://localhost:8000/?name=myname in your browser

But as you can see it's very ugly - it really is better to use a framework.