Skip to content

Instantly share code, notes, and snippets.

@bmanojlovic
Created November 20, 2017 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bmanojlovic/403c726235bb8132699b5ac12bac0eae to your computer and use it in GitHub Desktop.
Save bmanojlovic/403c726235bb8132699b5ac12bac0eae to your computer and use it in GitHub Desktop.
Generate base36 encoded host names (xip.io)
#!/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