♻️ refactoring

This commit is contained in:
Ingolf Wagner 2024-11-17 07:32:06 +07:00
parent 95037bb56c
commit 053268c75d
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B

View file

@ -9,6 +9,7 @@ import socketserver
import qrcode
import random
import string
from functools import partial
def get_current_ip_address():
@ -19,12 +20,10 @@ def get_current_ip_address():
return ip_address
# Function to generate a default username
def generate_default_username():
return "".join(random.choices(string.ascii_lowercase + string.digits, k=8))
# Function to generate a default password
def generate_default_password():
return "".join(random.choices(string.ascii_letters + string.digits, k=12))
@ -59,21 +58,18 @@ parser.add_argument(
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)
arguments = parser.parse_args()
# Change the working directory to serve the specified directory
os.chdir(expanded_directory)
os.chdir(os.path.expanduser(arguments.directory))
# Define a request handler with basic authentication
class AuthHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, username, passsword, *args, **kwargs):
encoded_bytes = base64.b64encode(f"{username}:{passsword}".encode("utf-8"))
# Convert bytes back to string
self.encoded_str = encoded_bytes.decode("utf-8")
self.base64_encoded_string = base64.b64encode(
f"{username}:{passsword}".encode("utf-8")
).decode("utf-8")
super().__init__(*args, **kwargs)
def do_AUTHHEAD(self):
@ -92,40 +88,30 @@ class AuthHandler(http.server.SimpleHTTPRequestHandler):
def check_auth(self, auth_header):
encoded_credentials = auth_header.split()[1]
return encoded_credentials == self.encoded_str
# decoded_credentials = base64.b64decode(encoded_credentials).decode("utf-8")
# username, password = decoded_credentials.split(":")
# return username == args.username and password == args.password
return encoded_credentials == self.base64_encoded_string
def save_qr_code(url):
# Generate the QR code
qr = qrcode.QRCode(
qr_code = 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")
qr_code.add_data(url)
qr_code.make(fit=True)
image = qr_code.make_image(fill_color="black", back_color="white").convert("RGB")
image.save("qrcode.png")
from functools import partial
# Set up the server with the AuthHandler and the defined port
auth_handler = partial(AuthHandler, args.username, args.password)
with socketserver.TCPServer(("", args.port), auth_handler) as httpd:
auth_handler = partial(AuthHandler, arguments.username, arguments.password)
with socketserver.TCPServer(("", arguments.port), auth_handler) as httpd:
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}/ < the page"
)
print(
f"http://{args.username}:{args.password}@{ip_address}:{args.port}/qrcode.png < qrcode"
)
save_qr_code(f"http://{args.username}:{args.password}@{ip_address}:{args.port}/")
server_url = f"http://{arguments.username}:{arguments.password}@{ip_address}:{arguments.port}"
print(f"{server_url} < server url")
print(f"{server_url}/qrcode.png < qrcode")
print("press Ctr-C to stop server")
save_qr_code(server_url)
httpd.serve_forever()