Converta valid Ip address to an Integer

”’

Eample

“10.0.0.1” => 167772161
”’

def checkValidAIp(addr):
list1 = map(int,addr.split(‘.’))
flag = false
if len(list1) == 4
for octet,index in enumerate(list1):
if index == 0:
assert octet > 0 and octet <= 255
elif index == 1:
assert octet >= 0 and octet <= 255
elif index == 2:
assert octet >= 0 and octet <= 255
elif index == 3:
assert octet >= 0 and octet <= 255

def ipToInt(addr):
if checkValidAIp(addr):
ip_list = map(int,addr.split(‘.’))

#Multiply the int by its octet positon
sum = ((2**24) * ip_list[0]) + ((2**16) * ip_list[1]) + ((2**8) * ip_list[2]) + ((2**0) * ip_list[3])
return sum
else:
raise ‘Invalid Input’

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s