claude-3-7-sonnet-20250219· Anthropic LMArena Human Preference 140K
show me how to build a simple web server from scratch in python, don't use libraries,…
- User
show me how to build a simple web server from scratch in python, don't use libraries, bare metal as much as possible
- Assistant
Here's a simple web server in Python without using external libraries, just using the built-in socket module:
import socket # Create a socket object server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Define host and port host = '127.0.0.1' # localhost port = 8080 # Bind the socket to the address server_socket.bind((host, port)) # Start listening for incoming connections (max 5 queued connections) server_socket.listen(5) print(f"Server running on http://{host}:{port}") # Response headers and content response_template = """HTTP/1.1 200 OK Content-Type: text/html; charset=utf-8 Connection: close <!DOCTYPE html> <html> <head> <title>Simple Python Server</title> </head> <body> <h1>Hello from Python!</h1> <p>You requested: {path}</p> </body> </html> """ # Main server loop while True: # Accept a connection client_socket, client_address = server_socket.accept() print(f"Connection from {client_address}") # Receive data from the client request_data = client_socket.recv(1024).decode('utf-8') # Parse the request to get the path request_lines = request_data.split('\n') if request_lines and len(request_lines[0].split()) >= 2: path = request_lines[0].split()[1] else: path = "/" # Send response response = response_template.format(path=path) client_socket.sendall(response.encode('utf-8')) # Close the connection client_socket.close()Would you like me to explain or break down this code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.