Last active
June 14, 2024 19:58
-
-
Save themorgantown/1f68ad2230480c51e6621c7d82db57cc to your computer and use it in GitHub Desktop.
On a MAC: Loads an SSL server using a self-signed URL on your local machine using the URL: yourcomputershostname.local served from the folder: ~/Desktop/document/ - first run: chmod +x localserver.sh so this file is executable. Python only version: https://gist.github.com/themorgantown/ad592aad05e29d0d69394daa4342447d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Install mkcert if not already installed | |
if ! command -v mkcert &> /dev/null | |
then | |
echo "mkcert not found. Installing mkcert..." | |
brew install mkcert | |
mkcert -install | |
fi | |
# Get the local hostname | |
myhost=$(scutil --get LocalHostName).local | |
# Navigate to the document folder | |
cd ~/Desktop/document | |
# Create a hidden directory for the certificates | |
mkdir -p .certs | |
# Add hostname to /etc/hosts if not already present | |
if ! grep -q "$myhost" /etc/hosts; then | |
echo "127.0.0.1 $myhost" | sudo tee -a /etc/hosts | |
fi | |
# Generate local certificate for the hostname | |
mkcert -cert-file .certs/$myhost.pem -key-file .certs/$myhost-key.pem "$myhost" | |
# Create a simple HTTPS server using Python | |
cat <<EOF > https_server.py | |
import http.server | |
import ssl | |
import os | |
server_address = ('', 443) | |
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler) | |
cert_dir = os.path.join(os.path.dirname(__file__), '.certs') | |
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) | |
context.load_cert_chain(certfile=os.path.join(cert_dir, '$myhost.pem'), keyfile=os.path.join(cert_dir, '$myhost-key.pem')) | |
httpd.socket = context.wrap_socket(httpd.socket, server_side=True) | |
print(f"Serving on https://$myhost") | |
httpd.serve_forever() | |
EOF | |
# Open the local HTTPS URL | |
open https://$myhost | |
# Run the HTTPS server | |
python3 https_server.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment