Python使用IPv6地址解析主机名 [英] Python resolve a host name with IPv6 address

查看:739
本文介绍了Python使用IPv6地址解析主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有没有办法使用python来解析仅在ipv6和/或解决ipv4和ipv6中的主机名的主机名?

I wonder if there is a way to use python to resolve a hostname that resolves only in ipv6 and/or for a hostname that resolves both in ipv4 and ipv6?

socket.gethostbyname() socket.gethostbyname_ex()不适用于ipv6解析。

socket.gethostbyname() and socket.gethostbyname_ex()does not work for ipv6 resolution.

这样做的一个虚拟方法是运行实际的linux主机命令并解析结果。有没有更好的方法呢?

A dummy way to do that is to run actual linux host command and parse the results. Is there any better way to do that?

谢谢,

推荐答案

socket.getaddrinfo 支持IPv6。您只需要将 family 设置为 AF_INET6

socket.getaddrinfo supports IPv6. You just need to set family to AF_INET6.

socket.getaddrinfo("example.com", None, socket.AF_INET6)

这篇关于Python使用IPv6地址解析主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆