This project is a simple Python web server capable of processing a single request. It demonstrates the basic principles of socket programming and HTTP request handling.
March, 2023
The web server performs the following tasks:
- Creates a connection socket when contacted by a client (browser).
- Receives the HTTP request from this connection.
- Parses the request to determine the specific file being requested.
- Retrieves the requested file from the server’s file system.
- Creates an HTTP response message consisting of the requested file preceded by header lines.
- Sends the response over the TCP connection to the requesting browser.
- If a browser requests a file that is not present in the server, the server returns a “404 Not Found” error message.
- Python 3.x
- Basic knowledge of Python and socket programming
webserver.py: The main server script.simpleWeb.html: Example HTML file to be served by the server.
- Place the
webserver.pyandsimpleWeb.htmlfiles in the same directory. - Open a terminal and navigate to the directory containing these files.
- Run the server script:
python3 webserver.py
- Determine the IP address of the host running the server (e.g.,
192.168.0.20). - From another device within the same network, open a web browser and enter the URL:
Replace
http://192.168.0.20:6789/simpleWeb.html
192.168.0.20with the actual IP address and6789with the port number used in the server script.
To test the server:
- Access an existing file:
- The contents of
simpleWeb.htmlshould be displayed in the browser.
- The contents of
- Access a non-existing file:
- The server should return a “404 Not Found” error message.
The main server code (webserver.py) is as follows:
from socket import *
import sys
serverPort = 6789
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('0.0.0.0', serverPort))
serverSocket.listen(1)
while True:
print('Ready to serve...')
connectionSocket, addr = serverSocket.accept()
try:
message = connectionSocket.recv(1024).decode()
requestParts = message.split()
if len(requestParts) > 1:
filename = requestParts[1]
with open(filename[1:]) as f:
outputdata = f.read()
header = 'HTTP/1.1 200 OK\nContent-Type: text/html\n\n'
connectionSocket.send(header.encode())
connectionSocket.send(outputdata.encode())
else:
raise IOError
except IOError:
header = 'HTTP/1.1 404 Not Found\nContent-Type: text/html\n\n'
try:
connectionSocket.send(header.encode())
error_message = "<html><body>404 Not Found</body></html>"
connectionSocket.send(error_message.encode())
except ConnectionAbortedError as e:
print(f'Error sending data: {e}')
finally:
connectionSocket.close()
serverSocket.close()
sys.exit()