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:
pythonimport 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) - User
Thank you for helping me out! Could you tell me how can I generate a self-signed certificate as you said?
- Assistant
Certainly, here are the steps to do so,
- Write down the Common Name (CN) for your SSL Certificate. The CN is the fully qualified name for the system that uses the certificate. For static DNS, use the hostname or IP address set in your Gateway Cluster (for example. 192.16.183.131 or dp1.acme.com).
- Run the following OpenSSL command to generate your private key and public certificate. Answer the questions and enter the Common Name when prompted.
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out certificate.pem - Review the created certificate:
openssl x509 -text -noout -in certificate.pem - Combine your key and certificate in a PKCS#12 (P12) bundle:
openssl pkcs12 -inkey key.pem -in certificate.pem -export -out certificate.p12 - Validate your P2 file.
openssl pkcs12 -in certificate.p12 -noout -info
Once the certificate has been created, it can be uplaoded or used in a keystore of your choice.
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)