将IP转换为16进制,用于IPv4-IPv6

# --*-- coding: utf-8 --*--
# create by xiaocaiji

while 1:
str_ip = input("input a IP:")
list_ip = str_ip.split('.')
if len(list_ip) < 4:
print("error IP")
x = 0
try:
for i in list_ip:
if int(i) > 256:
print("error IP")
x += 1
elif int(i) < 0 :
print("error IP")
x += 1
except ValueError:
print("error IP")
else:
if x > 0:
continue
a = hex(int(list_ip[0]) * 256 + int(list_ip[1]))
b = hex(int(list_ip[2]) * 256 + int(list_ip[3]))
a_new = a.replace('0x','')
b_new = b.replace('0x','')
while len(a_new) < 4:
a_new = '0' + a_new
#if len(a_new) == 4 :
# break
# print(a_new)
while len(b_new) < 4:
b_new = '0' + b_new
#if len(b_new) == 4 :
# break
# print(b_new)
print("%s:%s"%(a_new.upper(),b_new.upper()))





posted on 2019-04-25 09:09  泡面小网管  阅读(1713)  评论(0编辑  收藏  举报

导航