侧边栏壁纸
博主头像
新都在 博主等级

行动起来,活在当下

  • 累计撰写 175 篇文章
  • 累计创建 37 个标签
  • 累计收到 88 条评论

目 录CONTENT

文章目录

Python之DNS解析

Carol
2021-10-10 / 0 评论 / 0 点赞 / 20 阅读 / 0 字 / 正在检测是否收录...

😋特惠💰

多个付费教程捆绑包限时特惠,8折打包购买: 详情点击爱发电 ~~~

Python之DNS解析

  1. 实现A类型的域名DNS解析(剩下3种类型实现了基本过程,组装过程请自行实现)
  2. 根据DNS解析结果匹配对应的区域信息和运营商信息

实现代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json

import dns
import requests


def resolution_a(domain):
    query_list = []
    a = dns.resolver.resolve(domain, 'A')
    for i in a.response.answer:
        for j in i.items:
            if j.rdtype == 1:
                ip = j.address
                __ip_query = get_ip_info(ip)
                __ip_query["ip"] = ip
                __ip_query["domain"] = domain
                query_list.append(__ip_query)
    return query_list


def resolution_mx(domain):
    mx = dns.resolver.resolve(domain, 'MX')  # 指定查看类型为MX
    for i in mx:
        print('MX preference=', i.preference, 'mail exchanger=', i.exchange)


def resolution_ns(domain):
    ns = dns.resolver.resolve(domain, 'NS')  # 指定查询类型为NS记录
    for i in ns.response.answer:
        for j in i.items:
            print(j.to_text())


def resolution_cname(domain):
    cname = dns.resolver.resolve(domain, 'CNAME')  # 指定查询类型为CNAME记录
    for i in cname.response.answer:  # 结果将回应cname后的目标域名
        for j in i.items:
            print(j.to_text())


def get_ip_info(ip):
    # IP地址库接口
    r = requests.get('https://ip.taobao.com/getIpInfo.php?ip=%s' % ip)
    content = json.loads(r.content.decode("utf8"))
    ip_query = {}
    if "code" in content and content["code"] == '0':
        i = content['data']
        ip_query["country"] = i['COUNTRY_CN']
        ip_query["area"] = i['AREA_CN']
        ip_query["province"] = i['PROVINCE_CN']
        ip_query["city"] = i['CITY_CN']
        ip_query["isp"] = i['ISP_CN']
    return ip_query


if __name__ == '__main__':
    __domain = "baidu.com"
    resolution_a(__domain)
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区