Add lights.py

This commit is contained in:
jimmy 2024-07-05 11:06:15 +00:00
commit 3e06e91499
1 changed files with 37 additions and 0 deletions

37
lights.py Normal file
View File

@ -0,0 +1,37 @@
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()