Frage

Ich habe einen DNS-Skript, das auf einem Windows-Befehl durch Eingabe Website-Namen Benutzer zu lösen DNS-Namen erlaubt prompt.

Ich habe auf dem DNS-Auflösungs mehrere Führer sah durch, aber mein Skript kann nicht noch scheinen die Namen aufzulösen (www.google.com) oder (google.com) auf IP-Adresse.

Das Skript gibt einen Fehler von

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

Der Code:

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

Bitte geben Ratschläge zu den Codes. Dank!

War es hilfreich?

Lösung

Eingang () ist die falsche Funktion hier zu verwenden. Er wertet tatsächlich die Zeichenfolge, die der Benutzer eingegeben werden.

Auch gethostbyname_ex gibt mehr als nur einen String. So Ihre print-Anweisung auch gescheitert wäre.

In Ihrem Fall sollte dieser Code funktionieren:

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

Andere Tipps

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

Verwenden raw_input statt input.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top