Python DNS parsing and service detection

Source: Internet
Author: User
Tags http 200

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.