玩蛇网提供最新Python编程技术信息以及Python资源下载!

ipv4与ipv6地址如何转换的python解决办法

python 培训

ipv4与ipv6地址如何转换的python解决办法。要想知道ipv4与ipv6地址转换的问题,首先要了解它的概念。

ipv4与ipv6地址是什么简单的来说一下:
IPv4:是互联网协议的第四版,同时也是第一个被广泛使用,构成现今互联网技术的基石的协议;
IPv6:是互联网工程任务组设计的用于替代现行版本IPv4的下一代IP协议;
目前IP协议的版本号是IPv4,它的下一个版本就是IPv6。

python ipv4与ipv6地址转换

ipv4与ipv6地址如何转换的python解决办法,源码如下:(需要用到twisted.python.compat及struct和python socket方法)

import struct,socket
#import twisted.python.compat  #导入twisted.python.compat 更方便

# ipv4数字地址 
def ipv4_to_string(ipv4):
    ipv4_n = socket.htonl(ipv4)
    data = struct.pack('I', ipv4_n)
    ipv4_string = socket.inet_ntop(socket.AF_INET, data)
    return ipv4_string

def ipv4_from_string(ipv4_string):
    data = socket.inet_pton(socket.AF_INET, ipv4_string)
    ipv4_n = struct.unpack('I', data)
    ipv4 = socket.ntohl(ipv4_n[0])
    return ipv4

def ipv4_readable2int(ipv4):
    return int(ipv4)

def ipv4_int2readable(ipv4):
    return str(ipv4)

# ipv6用四个整数(tuple或用,分开的字符串)表示
def ipv6_to_string(ipv6):
    ipv6_n = (socket.htonl(ipv6[0]),
              socket.htonl(ipv6[1]),
              socket.htonl(ipv6[2]),
              socket.htonl(ipv6[3]))
    data = struct.pack('IIII', ipv6_n[0], ipv6_n[1], ipv6_n[2], ipv6_n[3])
    ipv6_string = socket.inet_ntop(socket.AF_INET6, data)

    return ipv6_string

def ipv6_from_string(ipv6_string):
    data = socket.inet_pton(socket.AF_INET6, ipv6_string)
    ipv6_n = struct.unpack('IIII', data)
    ipv6 = (socket.ntohl(ipv6_n[0]),
            socket.ntohl(ipv6_n[1]),
            socket.ntohl(ipv6_n[2]),
            socket.ntohl(ipv6_n[3]))

    return ipv6

def ipv6_tuple2readable(ipv6):
    return str(ipv6[0]) + ',' + str(ipv6[1]) + ',' + str(ipv6[2]) + ',' + str(ipv6[3])

def ipv6_readable2tuple(ipv6):
    return tuple(ipv6.split(','))

#win32 下实现 inet_pton 和 inet_ntop
def inet_ntop(family, ipstr):
    if family== socket.AF_INET:
        return socket.inet_ntoa(ipstr)
    elif family== socket.AF_INET6:
        v6addr = ':'.join(('%02X%02X' % (ord(i), ord(j)))
                         for i,j in zip(ipstr[::2], ipstr[1::2]))
        return v6addr

#www.iplaypython.com

def inet_pton(family, addr):
    if family== socket.AF_INET:
        return socket.inet_aton(addr)

    elif family== socket.AF_INET6:
        if '.' in addr: # a v4 addr
             v4addr = addr[addr.rindex(':')+1:]
             v4addr = socket.inet_aton(v4addr)
             v4addr = map(lambda x: ('%02X' % ord(x)), v4addr)
             v4addr.insert(2, ':')
             newaddr = addr[:addr.rindex(':')+1] + ''.join(v4addr)
             return inet_pton(family, newaddr)
        dbyts = [0]* 8 # 8 groups
        grps = addr.split(':')

        for i,v in enumerate(grps):
            if v:
                dbyts[i] = int(v, 16)
            else:
                for j, w in enumerate(grps[::-1]):
                    if w:
                        dbyts[7-j] = int(w, 16)
                    else:
                        break
                break
        return ''.join( (chr(i//256) + chr(i%256)) for i in dbyts)
 
   else:
        raise RuntimeError("What family?") 

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/security/se2434.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-04-11 11:13 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部