将IP转换为主机名 [英] convert IP to hostname

查看:145
本文介绍了将IP转换为主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个python脚本将IP地址转换为主机名.我正在使用Linux机器. 我在whois命令中看不到此信息.是否有任何命令始终能够提供正确的主机名(尽可能准确)?

I want to write a python script to convert IP addresses to hostnames. I am using a Linux box. I don't see this information in whois command. Are there any commands that always give the correct hostnames (as accurately as possible)?

推荐答案

Socket 库具有API进行反向DNS查找.

Socket library has an API to do reverse DNS lookups.

import socket
socket.gethostbyaddr("8.8.8.8")
>>> ('google-public-dns-a.google.com', [], ['8.8.8.8'])

请记住,并非所有IP地址都具有反向DNS条目,该查询的答案等中可能不会存在所有别名.

Keep in mind that not all IP addresses will have reverse DNS entries, not all aliases might be present in the answer to this query etc.

这篇关于将IP转换为主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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