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

查看:31
本文介绍了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天全站免登陆