zoukankan      html  css  js  c++  java
  • dnspython

    dnspython

    一个Python实现的一个DNS工具包,利用其查询功能来实现dns的服务监控及解析结果的校验。

    安装

    pip install dnspython

    解析域名为IP

    from dns import resolver
    
    
    f = open('ddd.log', 'w')
    
    with open('urls.txt', 'r') as fp:
        for line in fp:
            try:
                url = line[7:].strip()
                ans = resolver.query(url, 'A')	# A记录,将主机名转换为IP地址;
                
                # for x in ans.response.answer:	# 获取结果
        			# for y in x.items:
            			# print(y)
                t = url + '||Successful!'
            except Exception as e:
                t = url + '||' + str(e)
                print('resolver error!')
            finally:
                f.write(t + '
    ')
    f.close()
    

    urls.txt

    http://www.lyskjqbyjs.cn
    http://www.haict.edu.cn
    http://news.kf.cn
    http://www.henanjubao.com
    http://huixiangtan.smx.net.cn
    http://www.hnjnjc.gov.cn
    ...
    

    ddd.log

    www.lyskjqbyjs.cn||Successful!
    www.haict.edu.cn||None of DNS query names exist: www.haict.edu.cn., www.haict.edu.cn.
    news.kf.cn||Successful!
    www.henanjubao.com||Successful!
    huixiangtan.smx.net.cn||Successful!
    www.xcxnmj.gov.cn||None of DNS query names exist: www.xcxnmj.gov.cn., wx.hazz-l-
    www.yhgq.gov.cn||Successful!
    www.zzdangshi.com||Successful!
    smx.hnwzj.gov.cn||Successful!
    wfxy.ayit.edu.cn||Successful!
    www.sqsglj.com||All nameservers failed to answer the query www.sqsglj.com. IN A: Server 127.0.0.53 UDP port 53 answered The DNS operation timed out.; Server 127.0.0.53 UDP port 53 answered The DNS operation timed out.; Server 127.0.0.53 UDP port 53 answered The DNS operation timed out.; Server 127.0.0.53 UDP port 53 answered The DNS operation timed out.; Server 127.0.0.53 UDP port 53 answered The DNS operation timed out.; Server 127.0.0.53 UDP port 53 answered The DNS operation timed out.; Server 127.0.0.53 UDP port 53 answered SERVFAIL
    www.yyxsjj.gov.cn||Successful!
    ...
    

    socket包的gethostbyname(URL)也能解析IP地址,jd不推荐用socket,推荐dnspython。

  • 相关阅读:
    五种开源协议(GPL,LGPL,BSD,MIT,Apache)
    我想要做的编码机器人
    2017年前端框架、类库、工具大比拼
    Python实现好友全头像的拼接
    Python 中的闭包与装饰器
    Vuex2.0边学边记+两个小例子
    Vue.js写一个SPA登录页面的过程
    react与vue
    20行js代码制作网页刮刮乐
    css3动画详解
  • 原文地址:https://www.cnblogs.com/ldy-miss/p/10521895.html
Copyright © 2011-2022 走看看