如何在 Python 中进行 DNS 查找,包括引用/etc/hosts? [英] How can I do DNS lookups in Python, including referring to /etc/hosts?

查看:37
本文介绍了如何在 Python 中进行 DNS 查找,包括引用/etc/hosts?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dnspython 会很好地执行我的 DNS 查找,但它完全忽略了 /etc/的内容主机.

dnspython will do my DNS lookups very nicely, but it entirely ignores the contents of /etc/hosts.

是否有一个python库调用可以做正确的事情?即首先在 etc/hosts 中检查,否则只回退到 DNS 查找?

Is there a python library call which will do the right thing? ie check first in etc/hosts, and only fall back to DNS lookups otherwise?

推荐答案

我不确定您是要自己进行 DNS 查找,还是只想要主机的 ip.如果你想要后者,

I'm not really sure if you want to do DNS lookups yourself or if you just want a host's ip. In case you want the latter,

/! socket.gethostbyname 已弃用,首选 socket.getaddrinfo

/! socket.gethostbyname is depricated, prefer socket.getaddrinfo

来自man gethostbyname:

gethostbyname*()、gethostbyaddr*()、[...] 函数已过时.应用程序应该使用 getaddrinfo(3), getnameinfo(3),

The gethostbyname*(), gethostbyaddr*(), [...] functions are obsolete. Applications should use getaddrinfo(3), getnameinfo(3),

import socket
print(socket.gethostbyname('localhost')) # result from hosts file
print(socket.gethostbyname('google.com')) # your os sends out a dns query

这篇关于如何在 Python 中进行 DNS 查找,包括引用/etc/hosts?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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