#encoding:utf-8

import requests
import json
import dns.resolver
import sys
reload(sys)
sys.setdefaultencoding("utf-8") # 通过域名获取域名的A记录
def GetArecordIp(domain_name): address = []
try:
host_a = dns.resolver.query(domain_name, 'A')
for i in host_a.response.answer:
for j in i.items:
address.append(j.address)
return address
except:
return False # 通过调用淘宝的api根据ip获取城市
def get_city(ip_addr):
ip = ip_addr
# 通过淘宝的api查询ip信息
req = requests.get("http://ip.taobao.com/service/getIpInfo.php?ip={}".format(ip))
if req:
ip_data = json.loads(req.content)
ip_address = ip_data['data'] are = ''.join([ip_address['country'],ip_address['area'],ip_address['city']])
json.dumps(are)
# print are
return are def write_to_html(domain_ip):
# 通过拼凑ip和域名最终形成网页信息
# print domain_ip
title = "域名解析信息"
tbody = ''
for domain,ipcity in domain_ip.items():
print domain,ipcity[0][0],ipcity[0][1],ipcity[1][0],ipcity[1][1]
tbody += '<tr>\n<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n<tr>\n' % (domain,ipcity[0][0],ipcity[0][1],ipcity[1][0],ipcity[1][1]) html_tpl = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{title}</title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="0" color='pink'>
<thead>
<tr cellspacing="0" cellpadding="0">
<th>域名</th>
<th>ip信息1</th>
<th>ip所在城市1</th>
<th>ip信息2</th>
<th>ip所在城市2</th>
</tr>
</thead>
{tbody}
</table>
</body>
</html>
'''
html_handle = open('domain01.html','w')
html_handle.write(html_tpl.format(title = title, tbody = tbody))
html_handle.close() # 根据域名列表获取ip地址
def main(domain_file): f = open(domain_file,'r')
domain_list = f.readlines()
f.close() domain_ip = {}
for domain in domain_list:
domain_name = domain.rstrip()
ip_list = GetArecordIp(domain_name)
ip_city_list = [] # ip 不为空则处理
if ip_list:
for i in ip_list:
city = get_city(i)
ip_city = [i,city]
ip_city_list.append(ip_city)
# print json.dumps(ip_city_list).decode("unicode-escape")
# ip=','.join(str(i)for i in ip_city_list)
# json.dumps(ip_city_list).decode("unicode-escape")可以转为中文显示
domain_ip[domain_name] = ip_city_list
# print domain_name,json.dumps(ip_city_list).decode("unicode-escape")
else:
print "%s has no record" % domain_name # 将信息写入网页
write_to_html(domain_ip) if __name__ == '__main__':
domain_file = "domain_file.txt"
main(domain_file)

简单解析

#!/usr/bin/env python
#coding:utf-8 from socket import gethostbyname
DOMAIN= "ip02.txt" with open(DOMAIN,'r') as f: for line in f.readlines():
try:
host = gethostbyname(line.strip('\n')) #域名反解析得到的IP
except Exception as e:
with open('error.txt','a+') as ERR: #error.txt为没有IP绑定的域名
ERR.write(line.strip()+ '\n')
else:
with open('result.txt','a+') as r: #result.txt里面存储的是批量解析后的结果
r.write(line.strip('\n') + ' ') #显示有ip绑定的域名,用空格隔开
r.write(host + '\n')

python批量解析域名a记录的更多相关文章

  1. 批量解析域名为IP地址的python脚本

    脚本如下: #!/usr/bin/env python #coding:utf- import os,sys from socket import gethostbyname DOMAIN= &quo ...

  2. 解释-DNS,A记录,CNAME记录,域名转向,SRV记录,TTL值,泛域名与泛解析,域名绑定

    http://www.lihongye.net/post/dns.html DNS DNS,Domain Name System或者Domain Name Service(域名系统或者域名服务).域名 ...

  3. 聊聊host中ip/域名映射记录的解析规则

    今天宝叔突然在群里发了个问题; host做如下配置,a.com会指向哪里?或者说ping一下a.com结果会是什么? 127.0.0.1 a.com 192.168.4.106 a.com 192.1 ...

  4. 1元搭建自己的云服务器&解析域名

    最近在学做微信开发,没有自己的域名和服务器就不得不寄人篱下,索性自己就到云主机上搭建了个服务器,但是水平有限弄了一个下午~~有自己的域名和服务器的好处相信不用我多说了.比如日后可以有自己域名的个性博客 ...

  5. python高效解析日志入库

    python脚本解析日志文件入库一般有三个重要的步骤:读文件.解析文件.入库.在这三个方面下功夫,可确保我们获得最优的性能(这里不讨论并发) 1 读文件:一次读一行,磁盘IO太多,效率低下:一次性读如 ...

  6. DNS安全浅议、域名A记录(ANAME),MX记录,CNAME记录

    相关学习资料 http://baike.baidu.com/link?url=77B3BYIuVsB3MpK1nOQXI-JbS-AP5MvREzSnnedU7F9_G8l_Kvbkt_O2gKqFw ...

  7. 使用adns库解析域名

    1. adns.adns-python库简介 adns库是一个可进行异步非阻塞解析域名的库,主要使用C语言编写,在linux平台下运行.使用adns库进行域名解析效率非常,著名的开源网络爬虫larbi ...

  8. DNS安全浅议、域名A记录(ANAME),MX记录,CNAME记录(转)

    http://www.cnblogs.com/LittleHann/p/3828927.html 相关学习资料 http://baike.baidu.com/link?url=77B3BYIuVsB3 ...

  9. python批量修改linux主机密码

    +++++++++++++++++++++++++++++++++++++++++++标题:python批量修改Linux服务器密码时间:2019年2月24日内容:基于python实现批量修改linu ...

随机推荐

  1. golang使用redis

    redigo使用 手册地址:http://godoc.org/github.com/garyburd/redigo/redis github地址:https://github.com/garyburd ...

  2. 061、flannel的连通与隔离(2019-04-01 周一)

    参考https://www.cnblogs.com/CloudMan6/p/7447716.html   flannel网络连通性测试 不同host上的容器可以通过flannel网络进行通信,需要借助 ...

  3. termios.h(FreeBSD 12.0)

    一.文件位置 /usr/include/termios.h 二.文件内容 /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1 ...

  4. postfix 邮箱接收限制

    Postfix 限制 QQ 邮箱发送到 我的 test.com 域下邮箱. 1.编辑 main.cf  添加限制: smtpd_sender_restrictions = check_sender_a ...

  5. Linux之文件恢复[extundelete,针对rm]

    [恢复过程] 1.下载+安装extundelete cd /tmp wget wget http://jaist.dl.sourceforge.net/project/extundelete/extu ...

  6. 【译】第八篇 SQL Server安全数据加密

    本篇文章是SQL Server安全系列的第八篇,详细内容请参考原文. Relational databases are used in an amazing variety of applicatio ...

  7. 「PKUWC 2018」Minimax

    传送门:Here 一道线段树合并好题 如果要维护点$ x$的信息,相当于合并$ x$的两棵子树 对于这题显然有:任何叶子节点的权值都可能出现在其祖先上 因而我们只需要在线段树合并的时候维护概率即可 我 ...

  8. json 的类型

    json靠双引号与单引号, 区分是NUMBER 还是 STRING

  9. 【onclick事件】【改变 HTML 内容innerHTML】【图片替换】【改变标签的css】【判断输入是否是数字】

    1.onclick事件 <button type="button" onclick="alert('Welcome!')">点击这里</but ...

  10. Java概念(一)多态

    多态是一个行为具有不同的形式的能力: 多态是同一个接口,使用不同的实例执行不同操作 一.多态实现方式: 方式一.重写: 方式二.接口: 方式三.抽象类和抽象方法: