[PYTHON]Python Coding - DNS Resolver

标签: python

配上一张超级暖的图:

Friends with benifits


今天做的不多,尝试使用域名解析模块~~~

解析域名,查看相关网站状态,把ip写入数组,然后把域名和ip列表写入数据库~~~




import dns.resolver, mysql.connector, http.client


###Resolve Domain
ipslist = []                                 ###定义数组
def dnslookup(domain,dnstype):
    try:
        dnssearch = dns.resolver.query(domain, dnstype)
    except dns.resolver.NoAnswer:
        print ("没有DNS记录!")
        return
    except dns.resolver.NXDOMAIN:
        print ("没有DNS记录!")
    for i in dnssearch.response.answer:
        if dnstype == 'A' or dnstype == 'AAAA':
            for j in i.items:
                ipslist.append(j.address)    ###添加到数组
            return ipslist
        else:
            for j in i.items:
                ipslist.append(j)
            return ipslist


def fun_checkhttp(dnsname):
    getcontent = ""
    checkrq = http.client.HTTPConnection(dnsname)
    try:
        checkrq.request("GET", "/", "",{})
        checkrs = checkrq.getresponse()
        getcontent = int(checkrs.status)
    finally:
        if getcontent == 200 or getcontent == 301 or getcontent == 302 or getcontent == 307:
                                              #为什么不用正则?因为TMD不管用啊FUCK!!!
            context = "[OK]"
        else:
            context = "[ERROR]"
    return context


python_mysql = {
    'host': '172.18.8.62',
    'user': 'zhangky',
    'password': 'xinghuo844',
    'port': 3306,
    'database': 'python',
    'charset': 'utf8'
}


def insert_command(insert_cmd, param):
    """
    :param insert_cmd:
    :param param:
    """
    try:
        conn = mysql.connector.connect(**python_mysql)
    except mysql.connector.Error as error:
        print('连接失败!', format(error))
    cursor = conn.cursor()
    try:
        cursor.execute(insert_cmd,param)
        conn.commit()                           ###知识点如果数据库引擎为InnoDB时,必须使用commit
    except mysql.connector.Error as error:
        print('连接失败!', format(error))
    finally:
        cursor.close()
        conn.close()


domain = input('请输入域名:')
dnstype = input('请输入查询类型(A,MX,NS,PTR,CNAME):')
ips = str(dnslookup(domain=domain, dnstype=dnstype))
dnsname=domain
httprs = fun_checkhttp(domain)
insert_cmd = "insert into checkdns(dnsname, ips, status) values (%s, %s, %s);"
param = (dnsname, ips, httprs)
insert_command(insert_cmd=insert_cmd, param=param)


和前几天做的有点相似,有点用但不大,如果要做想做的东西看来有必要学习Python 网页编程。



版权所有:《Hoiwan's 》 => 《[PYTHON]Python Coding - DNS Resolver
本文地址:http://ihoiwan.com/?post=50
除非注明,文章均为 《Hoiwan's 》 原创,欢迎转载!转载请注明本文地址,谢谢。

发表评论: