将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()))





转载于:https://www.cnblogs.com/chen-wg/p/10766328.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值