Skip to content

Instantly share code, notes, and snippets.

@fractalbach
Created April 25, 2019 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fractalbach/c2bbfb6ebc2aafee31a20e864c7c6e63 to your computer and use it in GitHub Desktop.
Save fractalbach/c2bbfb6ebc2aafee31a20e864c7c6e63 to your computer and use it in GitHub Desktop.
Displays a URL as it's IP address in dot-decimal notation, binary, and decimal representations.
"""
URL to Binary to Decimal
How it Works
1. Does a DNS lookup on the URL given.
2. Converts the 255.255.255.255 into 4 binary numbers
3. Appends each of the binary numbers together
4. Converts the appended binary number into a decimal.
"""
DESCRIPTION = ''' Converts a URL into it's binary representation, and then into a
decimal number, which can be typed into your browser to reach a
website instead of the "normal" way.'''
import socket
import argparse
from string import Template
outputTemplate = Template(''' Converting URL...
URL : ${URL}
IP : ${IP}
Binary : ${BIN}
Decimal : ${DEC}
Try going to http://${DEC} in your browser!
''')
def main():
parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument(
metavar='URL',
nargs=1,
type=str,
help='URL string to inspect.',
dest='url',
)
args = parser.parse_args()
url = args.url[0]
processURL(url)
# byteBin returns a string containing the binary format of a number,
# similar to the bin() function, but up to 8 leading 0s, and without
# the '0b' at the beginning.
def byteBin(number):
return format(number, '08b')
# processURL takes a URL string, like 'python.org', and prints out the
# various representations that it can have. The most interesting one
# is the last, it's numerical format (in decimal). You can actually
# enter this into a web browser!
def processURL(URL):
ipAddress = socket.gethostbyname(URL)
byteStringArray = ipAddress.split('.')
byteArray = []
for byteString in byteStringArray:
n = int(byteString)
byteArray.append(n)
numericalAddress = byteArrayToNumber(byteArray)
binaryAddressString = format(numericalAddress, '032b')
output = outputTemplate.substitute({
'URL': URL,
'IP': ipAddress,
'BIN': binaryAddressString,
'DEC': numericalAddress,
})
print(output)
# converts an array of 4 numbers (the IP Address) into it's numerical
# representation, by appending each of the binary numbers together to
# form 1 giant number.
def byteArrayToNumber(arr):
n = 0
n |= arr[0] << 24
n |= arr[1] << 16
n |= arr[2] << 8
n |= arr[3]
return n
if __name__ == '__main__': main()
@fractalbach
Copy link
Author

Example Runs

$ python3 urlbin.py google.com
 Converting URL...
URL     : google.com
IP      : 172.217.6.78
Binary  : 10101100110110010000011001001110
Decimal : 2899904078

Try going to http://2899904078 in your browser!
$ python3 urlbin.py github.com
 Converting URL...
URL     : github.com
IP      : 192.30.255.113
Binary  : 11000000000111101111111101110001
Decimal : 3223256945

Try going to http://3223256945 in your browser!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment