import os import argparse import base64 import http.server import socket import socketserver import qrcode def get_current_ip_address(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) ip_address = s.getsockname()[0] s.close() return ip_address # Define the command-line arguments parser = argparse.ArgumentParser(description='Start a simple HTTP server with basic authentication.') parser.add_argument('--port', type=int, default = 8000, help = 'Port to serve the directory over.') parser.add_argument('--username', required=True, help='Username for basic authentication.') parser.add_argument('--password', required=True, help='Password for basic authentication.') parser.add_argument('--directory', default=os.getcwd(), help='Directory to serve. Defaults to the current directory.') args = parser.parse_args() # Expand the directory path (e.g., if it contains ~) expanded_directory = os.path.expanduser(args.directory) # Change the working directory to serve the specified directory os.chdir(expanded_directory) # Define a request handler with basic authentication class AuthHandler(http.server.SimpleHTTPRequestHandler): def do_AUTHHEAD(self): self.send_response(401) self.send_header('WWW-Authenticate', 'Basic realm="Server Access"') self.send_header('Content-type', 'text/html') self.end_headers() def do_GET(self): auth_header = self.headers.get('Authorization') if auth_header is None or not self.check_auth(auth_header): self.do_AUTHHEAD() self.wfile.write(b'Not authenticated') else: super().do_GET() def check_auth(self, auth_header): encoded_credentials = auth_header.split()[1] decoded_credentials = base64.b64decode(encoded_credentials).decode('utf-8') username, password = decoded_credentials.split(':') return username == args.username and password == args.password def save_qr_code(url): # Generate the QR code qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4, ) qr.add_data(url) qr.make(fit=True) # Create an image from the QR Code instance img = qr.make_image(fill_color='black', back_color='white').convert('RGB') # Save the image img.save("qrcode.png") # Set up the server with the AuthHandler and the defined port with socketserver.TCPServer(("", args.port), AuthHandler) as httpd: print(f"Serving the directory '{args.directory}' at http://localhost:{args.port}") print("qrcode.png is saved in the current directory.") ip_address = get_current_ip_address() print(f"http://{args.username}:{args.password}@{ip_address}:{args.port}/") save_qr_code(f"http://{args.username}:{args.password}@{ip_address}:{args.port}/") httpd.serve_forever()