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 a simple HTTPS file server in Python 3 that implements the requirements you specified:
import http.server import socketserver import ssl
HOSTNAME = "0.0.0.0" PORT = 8000 CERT_FILE = "server.pem" KEY_FILE = "key.pem" ALLOWED_CLIENT_IP = None
class CustomHandler(http.server.SimpleHTTPRequestHandler): def do_GET(self): if ALLOWED_CLIENT_IP and self.client_address[0] != ALLOWED_CLIENT_IP: self.send_error(403, "Forbidden", "You are not authorized to access this resource") return
- User
Thank you for helping me out! Could you add the explanation you gave me at the end to the code itself in the form of comments?
- Assistant
import http.server import socketserver import ssl
Server hostname and port to bind to
HOSTNAME = "0.0.0.0" PORT = 8000
SSL certificate and key files
CERT_FILE = "server.pem" KEY_FILE = "key.pem"
IP address of allowed client
ALLOWED_CLIENT_IP = None
Custom HTTP request handler
class CustomHandler(http.server.SimpleHTTPRequestHandler): def do_GET(self): # Check if request is from allowed client if ALLOWED_CLIENT_IP and self.client_address[0] != ALLOWED_CLIENT_IP: # Send "Forbidden" error if request is from different client self.send_error(403, "Forbidden", "You are not authorized to access this resource") return
code# Check if request is for download endpoint if self.path.endswith("/download"): # Update allowed client IP ALLOWED_CLIENT_IP = self.client_address[0] # Serve file to allowed client super().do_GET() else: # Send "Not Found" error if request is for other resource self.send_error(404, "Not Found", "The requested resource was not found on this server.")
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)