【问题标题】:How to resolve DNS in Python?如何在 Python 中解析 DNS?
【发布时间】:2011-04-19 18:15:02
【问题描述】:

我有一个 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')

请就代码提供建议。谢谢!

【问题讨论】:

    标签: python reverse-dns


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 很棒的答案伙伴!谢谢!但我不明白“repr(数据)”部分。介意给我解释一下吗?谢谢!
    • @JavaNoob: repr 返回一个包含对象可打印表示的字符串。 docs.python.org/library/functions.html#repr
    • 应该是repr(data[2]) - gethostbyname_ex() 返回一个数组,其第三个元素是IP地址。
    • 你也可以使用 socket.gethostbyname(x),它只返回 IP 地址而不是所有额外的。
    • getaddrinfo 可能有用 - gethostbyname_ex 仅限于 IPv4
    【解决方案2】:
    #!/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
    

    【讨论】:

      【解决方案3】:

      使用raw_input 代替input

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多