gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
hi
- User
hi
- Assistant
Hello! How can I assist you today?
- User
write code for chat app python
- Assistant
Sure, here is an example of a chat app in Python using the socket programming library:
import socket import threading # Define host and port HOST = 'localhost' PORT = 12345 # Create a socket object server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind socket to a specific address server_socket.bind((HOST, PORT)) # Start listening on the socket server_socket.listen() # List of connected clients clients = [] # Function to broadcast a message to all clients except the sender def broadcast(message, sender): for client in clients: if client != sender: client.send(message) # Function to handle client connections def handle_client(client_socket, client_address): print(f'New connection from {client_address}') # Add client to list of connected clients clients.append(client_socket) # Continuously receive and broadcast messages while True: try: # Receive message message = client_socket.recv(1024) if message: # Broadcast message to all clients broadcast(message, client_socket) else: # Remove client from list of connected clients clients.remove(client_socket) client_socket.close() break except: # Remove client from list of connected clients clients.remove(client_socket) client_socket.close() break # Continuously accept new connections and start a new thread for each connection while True: client_socket, client_address = server_socket.accept() thread = threading.Thread(target=handle_client, args=(client_socket, client_address)) thread.start()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.