如何在Python中解析DNS?

21 投票
3 回答
78340 浏览
提问于 2025-04-16 04:50

我有一个DNS脚本,可以让用户在Windows命令提示符下通过输入网站名称来解析DNS名称。

我查阅了好几份关于DNS解析的指南,但我的脚本还是无法把名称(比如www.google.com或google.com)转换成IP地址。

脚本输出了一个错误:

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

代码如下:

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')

请给我一些关于代码的建议。谢谢!

3 个回答

-3

raw_input 替代 input

21

在编程中,有时候我们需要处理一些数据,比如从一个地方获取数据,然后把它放到另一个地方。这个过程就像是把水从一个杯子倒到另一个杯子一样简单。

有些时候,我们会遇到一些问题,比如数据的格式不对,或者我们需要在处理数据时做一些额外的步骤。这就像是你在倒水的时候,发现杯子有个洞,水漏了出去,你就需要想办法修补这个洞,或者换一个新的杯子。

在编程里,我们可以使用一些工具和方法来帮助我们更好地处理这些数据。就像在厨房里,我们有不同的器具来帮助我们做饭,编程也有很多函数和库可以帮助我们完成任务。

总之,处理数据的过程可能会遇到各种各样的问题,但只要我们找到合适的方法,就能顺利完成任务。

#!/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
37

这里用input()这个函数是不对的。因为它会对用户输入的字符串进行评估。

另外,gethostbyname_ex返回的不仅仅是一个字符串。所以你的打印语句也会出错。

在你的情况下,这段代码应该可以正常工作:

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')  

撰写回答