我是靠谱客的博主 知性啤酒,最近开发中收集的这篇文章主要介绍python10进制转2进制掩码_python IPv6 十进制和十六进制互转,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1 #!/usr/bin/python

2 #-*- coding: UTF-8 -*-

3

4 importre5

6 defipv62dec(ipv6):7 ifcheckipv6(ipv6):8 compressIndex = ipv6.find('::')9 print("compressIndex:"+str(compressIndex))10

11 ipv4Index = ipv6.find('.')12 print("ipv4Index:"+str(ipv4Index))13

14 if compressIndex==-1 and ipv4Index==-1:15 print 'num: %d' %(noCompressipv62dec(ipv6))16 elif compressIndex!=-1 and ipv4Index==-1:17 print 'num: %d' %(compressipv62dec(ipv6))18 elif compressIndex==-1 and ipv4Index!=-1:19 ipv4,ipv6=ipv4v6Split(ipv6)20 if ipv4 andipv6:21 print 'num: %d' % (ipv42dec(ipv4)+noCompressipv62dec(ipv6))22 else:23 return ""

24 elif compressIndex != -1 and ipv4Index != -1:25 ipv4, ipv6 =ipv4v6Split(ipv6)26 if ipv4 andipv6:27 print 'num: %d' % (ipv42dec(ipv4)+compressipv62dec(ipv6))28 else:29 return ""

30 else:31 return ""

32 else:33 return ""

34

35 defipv42dec(ip):36 dec_value =037 v_list = ip.split('.')38 v_list.reverse()39 t = 1

40 for v inv_list:41 dec_value += int(v) *t42 t = t * (2 ** 8)43 returndec_value44

45 #Ipv6每段不补齐0 形成完整的4位,如2001:DB8:0:23:8:800:200C:417A,补齐为2001:0DB8:0000:0023:0008:0800:200C:417A

46 defipseg2str(ipseglist):47 ipstr=''

48 for ipseg inipseglist:49 if len(ipseg) == 1:50 ipseg = '000' +ipseg51 elif len(ipseg) == 2:52 ipseg = '00' +ipseg53 elif len(ipseg) == 3:54 ipseg = '0' +ipseg55 elif len(ipseg) == 4:56 ipseg =ipseg57 else:58 return ""

59 ipstr +=ipseg60 returnipstr61

62 ##没有0压缩的Ipv6转换为十进制,如2001:DB8:0:23:8:800:200C:417A

63 defnoCompressipv62dec(ipv6):64 iplist = ipv6.split(":")65 ifiplist:66 ipstr =ipseg2str(iplist)67 print(ipstr)68 return int(ipstr, 16)69 else:70 return ""

71

72 #带0压缩的ipv6转换为十进制,如FF01::1101

73 defcompressipv62dec(ipv6):74 compressList = ipv6.split("::")75 print("compressList:" +str(compressList))76 ipstr = ""

77 part1 =[]78 part2 =[]79 if len(compressList) == 2:80 part1 = compressList[0].split(":") if compressList[0] else[]81 part2 = compressList[1].split(":") if compressList[1] else[]82 if part1 orpart2:83 ipstr +=ipseg2str(part1)84 for i in range(8 - len(part1) -len(part2)):85 ipstr += '0000'

86 ipstr +=ipseg2str(part2)87 print("ipstr:" +ipstr)88 return int(ipstr, 16)89 else:90 return ""

91

92 #将内嵌ipv4的ipv6中的ipv6和ipv4分隔开,如FFFF::192.168.0.1 分隔为ipv4 192.168.0.1 ipv6 FFFF::0000:0000

93 defipv4v6Split(ipv6):94 ipv4str = ''

95 ipv6str = ''

96 ifcheckipv6(ipv6):97 list = ipv6.split(":")98 iflist:99 ipv4str = list[len(list) - 1]100 list.pop()101 ipv6str = ":".join(list) + ":0000:0000"

102 print("ipv4:" +ipv4str)103 print("ipv6:" +ipv6str)104 returnipv4str,ipv6str105

106 #校验ipv6的格式

107 defcheckipv6(ipv6):108 matchobj = re.match(r'^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*$',ipv6)109 ifmatchobj:110 returnTrue111 else:112 returnFalse113

114 ipv62dec('55555:5e:0:0:0:0:0:5668:eeee')115 ipv62dec('5555:5e:0:0:0:0:0:5668')116 ipv62dec('5555:5e::5668')117 ipv62dec('5555:5e::')118 ipv62dec('::5555:5e')119 ipv62dec('2001:DB8:0:23:8:800:192.168.0.1')120 ipv62dec('FFFF::192.168.0.1')121 ipv62dec('::192.168.0.1')122 ipv62dec('::FFFF:192.168.0.1')

最后

以上就是知性啤酒为你收集整理的python10进制转2进制掩码_python IPv6 十进制和十六进制互转的全部内容,希望文章能够帮你解决python10进制转2进制掩码_python IPv6 十进制和十六进制互转所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部