Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
要寻址的Python IPv6字节_Python_Ipv6_Pythonista - Fatal编程技术网

要寻址的Python IPv6字节

要寻址的Python IPv6字节,python,ipv6,pythonista,Python,Ipv6,Pythonista,我在iOS应用程序Pythonista3上运行python。 当我尝试获取IPv6地址时,它会结束 返回字节而不是格式化的地址。 现在,我正试图找到一种方法,要么不使用字节正确地获取地址,要么找到一种方法使字节成为地址。 以下是我运行以获取地址的代码: def getIPv6Addr(input): return socket.getaddrinfo(input, None, socket.AF_INET6) 结果如下: [(30, 2, 17, '', (30, '\x00\x00\

我在iOS应用程序Pythonista3上运行python。 当我尝试获取IPv6地址时,它会结束 返回字节而不是格式化的地址。 现在,我正试图找到一种方法,要么不使用字节正确地获取地址,要么找到一种方法使字节成为地址。 以下是我运行以获取地址的代码:

def getIPv6Addr(input):
    return socket.getaddrinfo(input, None, socket.AF_INET6)
结果如下:

[(30, 2, 17, '', (30, '\x00\x00\x00\x00\x00\x00&\x07\xf8\xb0@\x00\x08\x14')), (30, 1, 6, '', (30, '\x00\x00\x00\x00\x00\x00&\x07\xf8\xb0@\x00\x08\x14'))]
编辑:另一种解决方案是找到用于将数据转换为字节的编码类型

What Makes:
2607:f8b0:4000:814::200e
become
\x00\x00\x00\x00\x00\x00&\x07\xf8\xb0@\x00\x08\x14

如果您使用此软件包,有一种快速方法:

from django.utils.encoding import smart_str

a = '\x00\x00\x00\x00\x00\x00&\x07\xf8\xb0@\x00\x08\x14'


print(smart_str(a))

您可以使用python附带的
ipaddress
模块。只需将字节传递给
ipaddress.IPv6Address的构造函数
,您就会得到一个表示地址的对象,并为您提供许多打印和操作地址的可能性。

解决了! 我用十六进制对字节进行编码,结果 它变成了地址

>>> getIPv6Addr("google.com")[0][4][1].encode("hex")
'0000000000002607f8b040000811'

可能有关联?您得到的2元组显然不像任何指定IPv6地址的元组。我在Python 2中的socket.getaddrinfo或Linux上的Python 3中没有看到类似的结果。这让我大吃一惊。在尝试运行smart_str时,我发现pythonista+django无法工作,因为django调用子进程,而iOS阻塞了子进程。谢谢你的努力!