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 implements a basic server using the
socketlibrary. The server listens for incoming connections and replies with "pong" whenever it receives a "ping" message.pythonimport socket def start_ping_pong_server(host='127.0.0.1', port=65432): # Create a TCP/IP socket 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 conn, addr = server_socket.accept() with conn: print(f'Connected by {addr}') while True: data = conn.recv(1024) if not data: break message = data.decode('utf-8') print(f'Received: {message}') if message.strip().lower() == 'ping': response = 'pong' conn.sendall(response.encode('utf-8')) else: print('Unknown message, no response sent.') if __name__ == '__main__': start_ping_pong_server()How to Run the Script:
- Make sure you have Python installed on your machine.
- Save the code to a file named
ping_pong_server.py. - Run the script in your terminal or command prompt:
code
python ping_pong_server.py - The server will start and listen for incoming connections on
localhostat port65432.
Client Example
You can test this server using a simple client script:
python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.