Created
April 25, 2019 06:44
-
-
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.
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
""" | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Runs