Minimal HTTP Server with http.server
Python includes a complete web server in `http.server`. You can run a production-capable local web server in 3 lines of code or via terminal `python3 -m http.server 8000` 🖥️.
import http.server, socketserver
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
# To run server:
# with socketserver.TCPServer(("", PORT), Handler) as httpd:
# print(f"Serving HTTP on port {PORT} 🖥️")
# httpd.serve_forever()
print("Standard library includes http.server with built-in GET & HEAD handling 🖥️")
# 36. Minimal HTTP Server with http.server Python includes a complete web server in `http.server`. You can run a production-capable local web server in 3 lines of code or via terminal `python3 -m http.server 8000` 🖥️. ```python import http.server, socketserver PORT = 8000 Handler = http.server.SimpleHTTPRequestHandler # To run server: # with socketserver.TCPServer(("", PORT), Handler) as httpd: # print(f"Serving HTTP on port {PORT} 🖥️") # httpd.serve_forever() print("Standard library includes http.server with built-in GET & HEAD handling 🖥️") ``` This built-in tool is indispensable for previewing Astro builds, sharing files on local networks, or testing client-side WebAssembly apps! 🖥️ --- ### Question Which terminal command spins up an instant static file web server in your current working directory without installing any packages? 🧠 - [ ] a. `python3 -m http.server 8000` 🖥️ - [ ] b. `python3 run webserver` - [ ] c. `python3 --serve-html` - [ ] d. `python3 start nginx` --- ### Challenge Import `http.server`. Print whether `hasattr(http.server, 'SimpleHTTPRequestHandler')` is True. 🎯 <details> <summary>Solution</summary> ```python import http.server print(hasattr(http.server, "SimpleHTTPRequestHandler")) ``` </details>