-
Notifications
You must be signed in to change notification settings - Fork 0
/
claculadora.py
38 lines (26 loc) · 924 Bytes
/
claculadora.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
if len (sys.argv) != 2:
# Exit the script
sys.exit("Usage: ./ex1_binary_converter.py <ip_address>")
ip_addr = sys.argv.pop()
octets = ip_addr.split(".")
# create a blank list (needed because I use .append() method below)
ip_addr_bin = []
if len (octets) == 4:
for octet in octets:
bin_octet = bin (int(octet))
# strip off '0b' from front of string (you can slice a string also)
bin_octet = bin_octet [2:]
while True:
if len (bin_octet) >= 8:
break
bin_octet = '0' + bin_octet
# add octet to new list
ip_addr_bin.append(bin_octet)
# join binary number in dotted-binary format
ip_addr_bin = ".".join(ip_addr_bin)
# print the output
print ("\n%-15s %-45s" % ("IP address", "Binary"))
print ("%-15s %-45s\n\n" % (ip_addr, ip_addr_bin))
else:
sys.exit("Invalid IP address entered")