gpt-4o-mini-2024-07-18· openai
from WildChat-4.8M
give me script python /ping reply pong
- User
give me script python /ping reply pong
- Assistant
Certainly! Below is a simple Python script that uses a socket to create a basic server that listens for a "ping" message and replies with "pong".
pythonimport socket def ping_pong_server(): # Set up the server host = 'localhost' # Standard loopback interface (localhost) port = 12345 # Port to listen on (non-privileged ports are > 1023) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket: server_socket.bind((host, port)) server_socket.listen() print(f"Server listening on {host}:{port}") while True: # Wait for a connection client_socket, addr = server_socket.accept() with client_socket: print(f"Connection from {addr} has been established!") # Receive the data from client data = client_socket.recv(1024).decode('utf-8') print(f"Received: {data}") # Check if the message is "ping" if data == "ping": response = "pong" print(f"Sending: {response}") client_socket.sendall(response.encode('utf-8')) if __name__ == "__main__": ping_pong_server()Instructions to Run the Server:
- Save the code to a file, e.g.,
ping_pong_server.py. - Open a terminal and navigate to the directory where the file is saved.
- Run the server with the command:
bash
python ping_pong_server.py - The server will start and listen for connections on
localhostat port12345.
To Test the Server:
You can use a simple client script or a tool like
telnetto connect to the server and send a "ping" message. - Save the code to a file, e.g.,
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.