# Host a Website at Home # Iffy Books | June 2025 | wtfpl # https://iffybooks.net/host-a-website-at-home # This script checks whether your external IP address has # changed since last time it ran. If there's been a change, # we'll update the namecheap API with our new IP address. # Log in to namecheap and go to Domain List > Manage > Advanced DNS # Scroll down and select Dynamic DNS to get your DDNS password. # Run a command like this to save your DDNS password to a text file: # echo 'yourddnspass23j82d938j' > ~/ddns_pass.txt import os import subprocess import time from datetime import datetime # Change to user home directory os.chdir(os.path.expanduser('~/')) # Store date and time as a string current_time_string = str(datetime.now()) # Read DDNS password from ddns_pass.txt with open('ddns_pass.txt') as fi: ddns_pass = fi.read().strip() # Check current IP address using icanhazip.com current_ip_address = subprocess.check_output(["curl", "--ipv4", "icanhazip.com"]).decode('utf-8').strip() print("Current IP address: " + current_ip_address) # Read old IP address from a text file if it's available if 'old_ip_address.txt' in os.listdir(): with open('old_ip_address.txt') as fi: old_ip_address = fi.read().strip() # Add code to check whether this is a valid IP address print("Old IP address: " + old_ip_address) # If old IP address isn't available, write the current address to old_ip_address.txt else: print("No previous IP address found. Writing current IP address to disk") with open('old_ip_address.txt','w') as fi: fi.write(current_ip_address) # If the IP address has changed, update namecheap API if current_ip_address != old_ip_address: print("Your IP address has changed!") host = '@' domain_name = '40404.net' api_url = f"https://dynamicdns.park-your-domain.com/update?host={host}&domain={domain_name}&password={ddns_pass}&ip={current_ip_address}" print(api_url) # Calling API URL to update IP address subprocess.call(['curl', api_url]) # Write current IP address to disk with open('old_ip_address.txt','w') as fi: fi.write(current_ip_address) # If the IP address hasn't changed, do nothing else: print(f"IP address is unchanged at {current_time_string}. Moving on.") # More details on the namecheap DDNS API here: # https://www.namecheap.com/support/knowledgebase/article.aspx/29/11/how-to-dynamically-update-the-hosts-ip-with-an-https-request/