[Python] Ipv6 address format from struct.unpack

Discussão em 'Python' iniciado por Stack, Abril 13, 2025.

  1. Stack

    Stack Membro Participativo

    What would be the way to unpack and map to ipv6 address. I'm currently doing the following

    In [3]: struct.unpack("!h",'*\x00')
    Out[3]: (10752,)

    In [5]: ipaddress.IPv6Address(10752)
    Out[5]: IPv6Address(u'::2a00')


    but of course the end result i wish for is 2a00:: , i was expecting IPv6Address to return it but i'm missing something.

    What i'm currently doing is unpacking as sting then to hex and append :: .

    In [14]: struct.unpack("!2s",'*\x00')
    Out[14]: ('*\x00',)

    In [15]: '*\x00'.encode("hex")
    Out[15]: '2a00'

    then append to hex and append

    In [16]: '*\x00'.encode("hex")+'::'
    Out[16]: '2a00::'

    Continue reading...

Compartilhe esta Página