-
-
Save averonesis/a5b885503b350030bf243eb768aebf8e to your computer and use it in GitHub Desktop.
Generate base36 encoded host names (xip.io)
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
#!/usr/bin/python3 | |
import ipaddress | |
import sys | |
# SO one :) | |
def base36encode(number): | |
""" | |
Base 36 encoder function | |
""" | |
if not isinstance(number, (int)): | |
raise TypeError('number must be an integer') | |
if number < 0: | |
raise ValueError('number must be positive') | |
alphabet, base36 = ['0123456789abcdefghijklmnopqrstuvwxyz', ''] | |
while number: | |
number, i = divmod(number, 36) | |
base36 = alphabet[i] + base36 | |
return base36 or alphabet[0] | |
def base36decode(number): | |
""" | |
base 36 decode... | |
""" | |
return int(number, 36) | |
if len(sys.argv) < 2: | |
sys.exit('Usage: %s <IP address>' % sys.argv[0]) | |
IP = sys.argv[1].split('.') | |
if len(IP) < 4: | |
sys.exit('<IP address> not in correct form') | |
B36NAME = base36encode(int(ipaddress.ip_address( | |
u'{}.{}.{}.{}'.format(IP[3], IP[2], IP[1], IP[0])))) | |
print("IP {}\nXIP_b36 http://{}.xip.io\nXIP_SSL https://{}.xip.io\nXIP_plain http://{}.xip.io".format( | |
sys.argv[1], B36NAME, B36NAME, sys.argv[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment