python IPv6 十进制和十六进制互转

IPv6 转 十进制:

  1 #!/usr/bin/python
  2 # -*- coding: UTF-8 -*-
  3 
  4 import re
  5 
  6 def ipv62dec(ipv6):
  7     if checkipv6(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 and ipv6:
 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 and ipv6:
 27                 print 'num: %d' % (ipv42dec(ipv4)+compressipv62dec(ipv6))
 28             else:
 29                 return ""
 30         else:
 31             return ""
 32     else:
 33         return ""
 34 
 35 def ipv42dec(ip):
 36     dec_value = 0
 37     v_list = ip.split('.')
 38     v_list.reverse()
 39     t = 1
 40     for v in v_list:
 41         dec_value += int(v) * t
 42         t = t * (2 ** 8)
 43     return dec_value
 44 
 45 #Ipv6每段不补齐0 形成完整的4位,如2001:DB8:0:23:8:800:200C:417A,补齐为2001:0DB8:0000:0023:0008:0800:200C:417A
 46 def ipseg2str(ipseglist):
 47     ipstr=''
 48     for ipseg in ipseglist:
 49         if len(ipseg) == 1:
 50             ipseg = '000' + ipseg
 51         elif len(ipseg) == 2:
 52             ipseg = '00' + ipseg
 53         elif len(ipseg) == 3:
 54             ipseg = '0' + ipseg
 55         elif len(ipseg) == 4:
 56             ipseg = ipseg
 57         else:
 58             return ""
 59         ipstr += ipseg
 60     return ipstr
 61 
 62 ##没有0压缩的Ipv6转换为十进制,如2001:DB8:0:23:8:800:200C:417A
 63 def noCompressipv62dec(ipv6):
 64     iplist = ipv6.split(":")
 65     if iplist:
 66         ipstr = ipseg2str(iplist)
 67         print(ipstr)
 68         return int(ipstr, 16)
 69     else:
 70         return ""
 71 
 72 #带0压缩的ipv6转换为十进制,如FF01::1101
 73 def compressipv62dec(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 or part2:
 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 def ipv4v6Split(ipv6):
 94     ipv4str = ''
 95     ipv6str = ''
 96     if checkipv6(ipv6):
 97         list = ipv6.split(":")
 98         if list:
 99             ipv4str = list[len(list) - 1]
100             list.pop()
101             ipv6str = ":".join(list) + ":0000:0000"
102             print("ipv4:" + ipv4str)
103             print("ipv6:" + ipv6str)
104     return ipv4str,ipv6str
105 
106 #校验ipv6的格式
107 def checkipv6(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|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[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|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[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|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[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|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[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|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[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|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[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|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$',ipv6)
109     if matchobj:
110         return True
111     else:
112         return False
113 
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')

 

十进制 转 IPv6:

 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 
 4 import re
 5 
 6 #将ipv6中段中的0压缩,如005e 压缩为5e
 7 def subZero(ipseg):
 8     index=0
 9     for i in range(len(ipseg)):
10         if ipseg[i]=='0':
11             index+=1
12         else:
13             break
14     if index>=2:
15         return ipseg[index:] if ipseg[index:] else '0'
16     else:
17         return ipseg
18 
19 #将十进制数转换为ipv6
20 def dec2ipv6(dec):
21     if checkdec(dec) and int(dec)<=340282366920938463463374607431768211455:
22         hexstr=(hex(int(dec)))[2:]
23         hexstrlen = len(hexstr)
24         while hexstrlen<32:
25             hexstr='0'+hexstr
26             hexstrlen+=1
27         result=subZero(hexstr[0:4])+":"+subZero(hexstr[4:8])+":"+subZero(hexstr[8:12])+":"+subZero(hexstr[12:16])+":"+subZero(hexstr[16:20])+":"+subZero(hexstr[20:24])+":"+subZero(hexstr[24:28])+":"+subZero(hexstr[28:])
28         print("result:"+result)
29         return result
30     else:
31         return ""
32 
33 #校验十进制数字
34 def checkdec(dec):
35     matchobj = re.match(r'(0[dD])?[0-9]+$',dec)
36     if matchobj:
37         return True
38     else:
39         return False
40 
41 
42 dec2ipv6('340277174624079928635746076938671226881')
43 dec2ipv6('42540766411282593502542288130627076097')
44 dec2ipv6('281473913978881')
45 dec2ipv6('340277174624079928635746076938671226881')
46 dec2ipv6('113425732322140585886096426104944350824')

 

posted @ 2019-02-18 10:00  salami_china  阅读(3178)  评论(0编辑  收藏  举报