首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Python中解析DNS?

如何在Python中解析DNS?
EN

Stack Overflow用户
提问于 2010-10-01 16:57:59
回答 3查看 56.1K关注 0票数 14

我有一个DNS脚本,允许用户通过在Windows命令提示符上键入网站名称来解析DNS名称。

我已经查看了几个关于DNS解析的指南,但我的脚本似乎仍然无法将名称(www.google.com)或(google.com)解析为IP地址。

该脚本输出以下错误

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\python\main_menu.py", line 37, in ?
    execfile('C:\python\showdns.py')
  File "C:\python\showdns.py", line 3, in ?
    x = input ("\nPlease enter a domain name that you wish to translate: ")
  File "<string>", line 0, in ?
NameError: name 'google' is not defined

代码:

代码语言:javascript
复制
import socket

x = input ("\nPlease enter a domain name that you wish to translate: ")

print ("\n\nThe IP Address of the Domain Name is: "+socket.gethostbyname_ex(x))

x = raw_input("\nSelect enter to proceed back to Main Menu\n")
if x == '1': 
execfile('C:\python\main_menu.py')

请给出代码方面的建议。谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-10-01 17:06:21

这里使用的input()函数是错误的。它实际上计算用户输入的字符串。

此外,gethostbyname_ex返回的不仅仅是一个字符串。所以你的print语句也会失败。

在您的示例中,此代码应该可以工作:

代码语言:javascript
复制
import socket

x = raw_input ("\nPlease enter a domain name that you wish to translate: ")  

data = socket.gethostbyname_ex(x)
print ("\n\nThe IP Address of the Domain Name is: "+repr(data))  

x = raw_input("\nSelect enter to proceed back to Main Menu\n")  
if x == '1':   
    execfile('C:\python\main_menu.py')  
票数 29
EN

Stack Overflow用户

发布于 2013-02-23 06:27:47

代码语言:javascript
复制
#!/user/bin/env python
"""
Resolve the DNS/IP address of a given domain
data returned is in the format:
(name, aliaslist, addresslist)
@filename resolveDNS.py
@version 1.01 (python ver 2.7.3)
@author LoanWolffe
"""
import socket

def getIP(d):
    """
    This method returns the first IP address string
    that responds as the given domain name
    """
    try:
        data = socket.gethostbyname(d)
        ip = repr(data)
        return ip
    except Exception:
        # fail gracefully!
        return False
#
def getIPx(d):
    """
    This method returns an array containing
    one or more IP address strings that respond
    as the given domain name
    """
    try:
        data = socket.gethostbyname_ex(d)
        ipx = repr(data[2])
        return ipx
    except Exception:
        # fail gracefully!
        return False
#
def getHost(ip):
    """
    This method returns the 'True Host' name for a
    given IP address
    """
    try:
        data = socket.gethostbyaddr(ip)
        host = repr(data[0])
        return host
    except Exception:
        # fail gracefully
        return False
#
def getAlias(d):
    """
    This method returns an array containing
    a list of aliases for the given domain
    """
    try:
        data = socket.gethostbyname_ex(d)
        alias = repr(data[1])
        #print repr(data)
        return alias
    except Exception:
        # fail gracefully
        return False
#

# test it

x = raw_input("Domain name or IP address? > ")


a = getIP(x)
b = getIPx(x)
c = getHost(x)
d = getAlias(x)

print " IP ", a
print " IPx ", b
print " Host ", c
print " Alias ", d
票数 13
EN

Stack Overflow用户

发布于 2010-10-01 17:04:19

使用raw_input而不是input

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3837744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档