2010-10-01 32 views
10

Tôi có một kịch bản DNS cho phép người dùng phân giải tên DNS bằng cách nhập tên trang web trên dấu nhắc lệnh Windows.Cách giải quyết DNS bằng Python?

Tôi đã xem qua một số hướng dẫn về giải pháp DNS nhưng tập lệnh của tôi vẫn không thể giải quyết tên (www.google.com) hoặc (google.com) thành địa chỉ IP.

Kịch bản kết quả đầu ra một lỗi của

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 

Mã:

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

hãy đưa ra lời khuyên về những mật mã. Cảm ơn!

Trả lời

19

đầu vào() là chức năng sai để sử dụng tại đây. Nó thực sự đánh giá chuỗi mà người dùng đã nhập.

Cũng gethostbyname_ex trả về nhiều hơn chỉ là một chuỗi. Vì vậy, tuyên bố in của bạn cũng sẽ thất bại.

Trong trường hợp của bạn mã này nên làm việc:

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

tạo ảnh vui nhộn câu trả lời người bạn đời! Cảm ơn! Nhưng tôi không hiểu phần "repr (data)". Tâm trí giải thích cho tôi? Cảm ơn! – JavaNoob

+1

@JavaNoob: 'repr' trả về một chuỗi chứa biểu diễn có thể in của đối tượng. http://docs.python.org/library/functions.html#repr –

+1

Nên được repr (dữ liệu [2]) - gethostbyname_ex() trả về một mảng, có phần tử thứ ba là địa chỉ IP. – ripper234

-1

Sử dụng raw_input thay vì input.

9
#!/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 
Các vấn đề liên quan