0
0
PythonHow-ToBeginner · 3 min read

How to Create a Simple HTTP Server in Python Quickly

You can create a simple HTTP server in Python using the http.server module by running python -m http.server in your terminal. For more control, write a script that imports http.server and uses HTTPServer and SimpleHTTPRequestHandler classes to start the server.
📐

Syntax

The basic syntax to start a simple HTTP server in Python involves importing the http.server module and creating an HTTPServer instance with a handler like SimpleHTTPRequestHandler. Then, call the serve_forever() method to keep the server running.

  • HTTPServer: Creates the server object that listens on a given address and port.
  • SimpleHTTPRequestHandler: Handles incoming HTTP requests and serves files from the current directory.
  • serve_forever(): Starts the server loop to handle requests continuously.
python
from http.server import HTTPServer, SimpleHTTPRequestHandler

server_address = ('', 8000)  # Listen on all interfaces, port 8000
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
httpd.serve_forever()
💻

Example

This example creates a simple HTTP server that serves files from the current directory on port 8000. You can open http://localhost:8000 in your browser to see the files.

python
from http.server import HTTPServer, SimpleHTTPRequestHandler

server_address = ('', 8000)  # Listen on all interfaces, port 8000
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
print('Serving HTTP on port 8000...')
httpd.serve_forever()
Output
Serving HTTP on port 8000...
⚠️

Common Pitfalls

Common mistakes when creating a simple HTTP server in Python include:

  • Not specifying the correct port or using a port already in use.
  • Running the server without proper permissions to bind to the port.
  • Expecting the server to handle complex web applications; it only serves static files.
  • Forgetting to stop the server properly (use Ctrl+C in terminal).

Also, using python -m http.server is a quick way but offers less control than writing a script.

python
import socket

# Wrong: Using a port below 1024 without admin rights
server_address = ('', 80)  # This may cause permission error

# Right: Use a port above 1024
server_address = ('', 8000)
📊

Quick Reference

Summary tips for creating a simple HTTP server in Python:

  • Use python -m http.server for a quick server in the current directory.
  • Write a script with HTTPServer and SimpleHTTPRequestHandler for more control.
  • Choose ports above 1024 to avoid permission issues.
  • Stop the server with Ctrl+C in the terminal.

Key Takeaways

Use the built-in http.server module to create a simple HTTP server quickly.
Run 'python -m http.server' in your terminal for an instant server serving current directory files.
For more control, write a Python script using HTTPServer and SimpleHTTPRequestHandler classes.
Choose ports above 1024 to avoid permission errors when binding the server.
Stop the server safely with Ctrl+C to free the port and end the process.