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

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

问题描述

dnspython 将非常好地进行DNS查找,但完全忽略的内容/ etc / hosts

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,

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天全站免登陆