38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
|
import socket
|
||
|
|
||
|
# Set up the server
|
||
|
def start_server():
|
||
|
host = '0.0.0.0' # Localhost
|
||
|
port = 80 # Port to listen on
|
||
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||
|
s.bind((host, port))
|
||
|
s.listen(5)
|
||
|
print(f'Serving HTTP on {host} port {port} (http://{host}:{port}/) ...')
|
||
|
|
||
|
while True:
|
||
|
conn, addr = s.accept()
|
||
|
request = conn.recv(1024).decode('utf-8')
|
||
|
print(f'Connected by {addr}\nRequest:\n{request}')
|
||
|
|
||
|
# Check if request is for the root path
|
||
|
if "GET /a " in request:
|
||
|
response = f'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\na'
|
||
|
elif "GET /b " in request:
|
||
|
response = f'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nb'
|
||
|
elif "GET /c " in request:
|
||
|
response = f'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nc'
|
||
|
elif "GET /d " in request:
|
||
|
response = f'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nd'
|
||
|
elif "GET /e " in request:
|
||
|
response = f'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\ne'
|
||
|
else:
|
||
|
response = 'HTTP/1.1 404 Not Found\r\n\r\n'
|
||
|
|
||
|
conn.sendall(response.encode('utf-8'))
|
||
|
conn.close()
|
||
|
|
||
|
# Start the server
|
||
|
if __name__ == '__main__':
|
||
|
start_server()
|
||
|
|