Write a simple Python3 HTTPS file server that waits for a connection from a client, and…
- User
Write a simple Python3 HTTPS file server that waits for a connection from a client, and once receiving a connection, will only allow further connections from that single connecting IP address. Make the server reply with an error page if any other user attempts to connect. Also make the server only allow the files served to be downloaded once, instead of allowing repeated downloads.
- Assistant
Here is an example code for an HTTPS file server in Python3 that meets the requirements you specified:
import socket import ssl import os HOST = '0.0.0.0' PORT = 443 CERT_FILE = 'cert.pem' KEY_FILE = 'key.pem' ALLOWED_IP = None FILE_NAME = 'file.txt' def handle_client(conn, addr): global ALLOWED_IP if ALLOWED_IP and addr[0] != ALLOWED_IP: conn.sendall(b'403 Forbidden') conn.close() return ALLOWED_IP = addr[0] if os.path.exists(FILE_NAME): with open(FILE_NAME, 'rb') as f: file_data = f.read() os.remove(FILE_NAME) conn.sendall(file_data) else: conn.sendall(b'404 Not Found') conn.close() context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) context.load_cert_chain(certfile=CERT_FILE, keyfile=KEY_FILE) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind((HOST, PORT)) s.listen() while True: conn, addr = s.accept() conn = context.wrap_socket(conn, server_side=True) handle_client(conn, addr)Note: You'll need to provide your own SSL certificate and key, which you can obtain through a certificate authority or generate a self-signed certificate. In this example, the certificate and key are expected to be stored in cert.pem and key.pem, respectively.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →