make me a python reverse proxy
- User
make me a python reverse proxy
Assistant
Here is some Python code for a basic reverse proxy:
import socket import select import sys # Set up constants HOST = '' PORT = 8888 TARGET_HOST = '127.0.0.1' TARGET_PORT = 80 BUFFER_SIZE = 4096 # Create the main socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind((HOST, PORT)) server_socket.listen(10) # Dict to store client sockets socket_list = {} while True: readable, _, _ = select.select([server_socket] + list(socket_list), [], []) for current_socket in readable: if current_socket is server_socket: # Handle new connection client_socket, client_address = current_socket.accept() socket_list[client_socket] = (client_address, None) print("Client connected: ", client_address) else: # Handle data from client client_address, target_socket = socket_list[current_socket] data = current_socket.recv(BUFFER_SIZE) if data: if target_socket is None: # Connect to target target_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) target_socket.connect((TARGET_HOST, TARGET_PORT)) socket_list[current_socket] = (client_address, target_socket) # Send data to target target_socket.send(data) else: # Client disconnected print("Client disconnected: ", client_address) current_socket.close() target_socket.close() del socket_list[current_socket]
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.