This is my HTTP server for a college project. It’s a simple web server made with Python that handles GET and POST requests, serves files like HTML, images, and text, and saves JSON data. It uses threads to handle multiple users at the same time.
-
Make sure you have Python 3 installed.
-
Save the code in a file called
http_server.py. -
Create a folder named
stuffin the same place as the code. -
Put some files in
stuff:- At least 3 HTML files (like
index.html,about.html,contact.html). - At least 2 PNG images (one should be bigger than 1MB).
- At least 2 JPEG images.
- At least 2 text files (
.txt).
- At least 3 HTML files (like
-
Open a terminal and go to the folder with the code.
-
Run the server with:
python http_server.pyYou can also add options:
python http_server.py 8000to use a different port.python http_server.py 8000 0.0.0.0to set host.python http_server.py 8000 0.0.0.0 20to set number of threads.
-
Open a browser and go to
http://localhost:8080to see it work! -
To stop, press
Ctrl+C.
- GET Requests:
- Shows HTML files (like
index.htmlfor/). - Downloads images (PNG, JPEG) and text files as files.
- Only allows
.html,.txt,.png,.jpg,.jpeg.
- Shows HTML files (like
- POST Requests:
- Takes JSON data at
/uploadand saves it as a file instuff/uploads. - Files are named like
upload_20231010_123456_abcd.json.
- Takes JSON data at
- Security:
- Blocks bad paths (like
../) to keep files safe. - Checks the
Hostheader to avoid bad requests. - Only accepts JSON for POST.
- Blocks bad paths (like
- Threads: Uses 10 threads by default to handle many users. Queues extra connections if busy.
- Connections: Supports keep-alive for HTTP/1.1, closes after 30 seconds or 100 requests.
project/
├── http_server.py
├── stuff/
│ ├── index.html
│ ├── about.html
│ ├── contact.html
│ ├── sample.txt
│ ├── logo.png
│ ├── photo.jpg
│ └── uploads/ (for POST files)
- Basic Tests:
- Visit
http://localhost:8080to seeindex.html. - Try
/about.htmlor/logo.pngto download files. - Send JSON to
/uploadusing a tool like Postman. - Try a bad file like
/something.exe(should fail with 415).
- Visit
- Security Tests:
- Try
/../etc/passwd(should get 403). - Send a request with wrong
Host(should get 403).
- Try
- Concurrency:
- Open multiple browser tabs to download files at once.
- Test with big files (>1MB) to check they download fine.
- Testing with Telnet:
You can use
telnetto test the server manually:- Start the server (
python http_server.py). - Open a terminal and type:
telnet localhost 8080 - Type an HTTP request (hit Enter twice at the end):
GET /index.html HTTP/1.1 Host: localhost:8080- Should return the HTML content of
index.html.
- Should return the HTML content of
- Try a file download:
GET /logo.png HTTP/1.1 Host: localhost:8080- Should return binary data with headers like
Content-Type: application/octet-stream.
- Should return binary data with headers like
- Test a bad path:
GET /../etc/passwd HTTP/1.1 Host: localhost:8080- Should return
403 Forbidden.
- Should return
- Test a wrong host:
GET /index.html HTTP/1.1 Host: evil.com- Should return
403 Forbidden.
- Should return
- Test a POST request (harder with telnet, better with Postman):
POST /upload HTTP/1.1 Host: localhost:8080 Content-Type: application/json Content-Length: 27 {"test":"hello"}- Should return
201 Createdwith a JSON response.
- Should return
- Start the server (
- Only supports GET and POST (no PUT, DELETE, etc.).
- Only allows specific file types (html, txt, png, jpg).
- JSON uploads can’t be bigger than 5MB.
- If too many people connect, some might get a “busy” error (503).
- Files are read in binary mode to keep them safe.
- Uses 8KB chunks for sending files to make it fast.
- Logs everything with timestamps to see what’s happening.
- Code is on my GitHub: [insert your GitHub link here].