This project implements a basic HTTP server in Python capable of handling file requests (GET and POST), supporting content encoding (gzip), and serving responses based on the requested paths.
- Setup and Installation
- Running the Server
- Server Features
- Troubleshooting 404 Errors for File Requests
-
Clone the repository or download the code files.
-
Ensure you have Python 3.x installed.
-
Install any dependencies (if required, you can create a virtual environment):
python3 -m venv venv source venv/bin/activate
-
No additional dependencies are required for this server.
To run the server, use the following command, passing the directory you want to serve files from:
python server.py /path/to/your/files
Replace /path/to/your/files
with the actual path where your files are located.
The server will start running on localhost
at port 4221
.
- Root Path (
/
): Returns a simple 200 OK response. - Echo Path (
/echo/{text}
): Echoes the text provided in the URL. - User-Agent Path (
/user-agent
): Returns theUser-Agent
from the request headers. - File Handling (
/files/{filename}
):- GET: Retrieves the specified file from the server directory.
- POST: Saves the content provided in the request body to the specified file in the server directory.
To retrieve a file (e.g., test.txt
):
curl http://localhost:4221/files/test.txt
To upload content to a file (e.g., test.txt
):
curl -X POST http://localhost:4221/files/test.txt -d "This is a test content"
If you're receiving a 404 error when attempting to access files (e.g., GET /files/test.txt
) via the server, follow these steps to troubleshoot the issue:
Ensure that the file you're trying to access is located in the directory specified when starting the server. For example, if you started the server with the following command:
python server.py /home/deepanshu/Documents/HTTP-Server-Python/
The server will attempt to serve files from the /home/deepanshu/Documents/HTTP-Server-Python/
directory. Verify that the file you're trying to access (test.txt
in this case) is in that directory.
You can check the file's existence with the following command:
ls /home/deepanshu/Documents/HTTP-Server-Python/test.txt
If the file does not exist, you need to place it in the specified directory.
Ensure the file has the appropriate read permissions for the server to access it. You can check the file's permissions with:
ls -l /home/deepanshu/Documents/HTTP-Server-Python/test.txt
Make sure the file has read permissions for the user running the server.
Example output should show something like this:
-rw-r--r-- 1 user user 1234 Dec 10 10:00 /home/deepanshu/Documents/HTTP-Server-Python/test.txt
If the file does not have the correct permissions, you can update the permissions with:
chmod 644 /home/deepanshu/Documents/HTTP-Server-Python/test.txt
The server code attempts to open the file using the following logic:
filename = path[7:] # Removes '/files/' prefix
This means that if the request is GET /files/test.txt
, the server will look for a file named test.txt
in the directory specified at startup. To help debug, add logging to verify the file path that the server is constructing:
print(f"Requested file path: {os.path.join(directory, filename)}")
This will help you confirm that the path the server is trying to access is correct.
If you're unsure whether the file is accessible, you can test accessing it directly from the terminal using cat
to ensure the content is readable:
cat /home/deepanshu/Documents/HTTP-Server-Python/test.txt
If the command successfully outputs the file's content, then the file exists and is readable.
Double-check that the directory you're passing to the server actually exists. If you provide a non-existent directory, the server will not be able to find any files to serve. You can verify the directory with:
ls /home/deepanshu/Documents/HTTP-Server-Python/
If the server encounters any issues reading the file, it will return a 500 Internal Server Error
. If the file is not found, it will return a 404 Not Found
. Look at the server logs for any error messages that might provide more insight into the problem.
By following these steps, you should be able to resolve the 404 errors when accessing files through the server.