Hello,
This is a test HTTP server.
Your request came from {ip}.
Have a nice day!
#!/usr/bin/env python3 """Simple HTTP server that responds 200 with a test message including client IP. Behavior: - GET returns HTTP/1.1 200 with a message including the requester IP - HEAD returns the same headers as GET but no body - If User-Agent contains "curl" or "wget" (case-insensitive) the server responds with Content-Type: text/plain; otherwise Content-Type: text/html. - --look controls which HTML variant is returned for non-CLI agents: * basic - plain HTML with no external references * nice - includes Google Font "Noto Sans" (default) * bootstrap - Bootstrap 5 layout - The URL query parameter "look" (e.g. /?look=nice) overrides the command-line --look for that request only. Values are case-insensitive and must be one of basic,nice,bootstrap. Usage: python3 ok_server.py # binds 0.0.0.0:8000, nice look python3 ok_server.py --look basic python3 ok_server.py -b 127.0.0.1 -p 8080 --look bootstrap Test: curl -i http://localhost:8000/ # text/plain for curl curl -i "http://localhost:8000/?look=basic" # request-level override curl -I http://localhost:8000/ # HEAD (headers only) wget -S -O - http://localhost:8000/ # text/plain for wget open http://localhost:8000/ # browser gets HTML variant per look """ import argparse import sys from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from typing import Tuple from urllib.parse import urlparse, parse_qs PLAIN_TEMPLATE = ( "Hello, This is a test HTTP server.\n\n" "Your request came from {ip}.\n\n" "Have a nice day!\n" ) HTML_BASIC = """
This is a test HTTP server.
Your request came from {ip}.
Have a nice day!
""" HTML_NICE = """This is a test HTTP server.
Your request came from {ip}.
Have a nice day!
This is a test HTTP server.
Your request came from {ip}.
Have a nice day!