Recently looking at Liu Tians's Python automation operations, according to Ms. Liu's ideas, record a DNS polling and service detection chestnut, as a learning note.
#!/usr/bin/env python
Import Dns.resolver
Import OS
Import Httplib
IPList = []//define an empty list to hold the resolved IP address
AppDomain = "www.baidu.com"//define Business domain name
def get_iplist (domain= "")://Establish an IP address function method to obtain DNS resolution
try:a = dns.resolver.query (domain, ' a ')//a record of resolving domain names
Except Exception,e:
Print "DNS resolver error:" +str (E)
Return
For I in A.response.answer://resolves the address of a record
For J in I.items:
Iplist.append (j.address)//Add the acquired address to the list
Return True
def checkip (IP):
checkurl=ip+ ": 80"//check whether the IP server's 80 port service is healthy
Httplib.socket.setdefaulttimeout (5)//define HTTP Link timeout time is 5 seconds
Conn=httplib. Httpconnection (Checkurl)//Create an HTTP link object
Try
Conn.request ("GET", "/", headers = {"Host": appdomain})//Initiate URL request, add Main head
R=conn.getresponse ()
GetContent = R.read (15)//Gets the first 15 characters of the URL page to make the availability check
Finally
If getcontent== "<!doctype html>"://the contents of the Monitoring URL page are generally defined beforehand
Print ip+ "[OK]"//HTTP 200 status
Else
Print ip+ "[Error]"
If __name__== "__main__":
If Get_iplist (AppDomain) and Len (iplist) >0://condition, domain name resolution returns at least one IP
For IP in IPList:
Checkip (IP)
Else
Print "DNS resolver error."
Run Result:
14.215.177.37 [OK]
This article is from the "My_soul" blog, make sure to keep this source http://soul455879510.blog.51cto.com/6180012/1893536
Python DNS parsing and service detection