#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. 介绍3款Markdown编辑器

    为什么写此篇  自从CSDN的博客有了Markdown后,慢慢的了解并学会了用Markdown语法写博客.但CSDN博客是在浏览器中使用,于是一直寻找离线的Markdown编辑器.  网上先是找到了M ...

  2. GBK 字符集

    什么是 GBK ? 中文名 汉字编码字符集 外文名 Chinese Internal Code Specification 全    称 <汉字内码扩展规范> GBK编码,是对GB2312 ...

  3. Mysql下Limit注入方法(此方法仅适用于5.0.0<mysql<5.6.6的版本)

    SQL语句类似下面这样:(此方法仅适用于5.0.0<mysql<5.6.6的版本) SELECT field FROM table WHERE id > 0 ORDER BY id ...

  4. Docker 查看容器 IP 地址

    查看Docker的底层信息. docker inspect 会返回一个 JSON 文件记录着 Docker 容器的配置和状态信息 docker inspect NAMES # 查看容器所有状态信息: ...

  5. Docker 更改默认存储目录 - 十一

    Cemtos 7 Docker 默认目录是 /var/lib/docker docker info 查看 docker 配置信息 更改 docker 默认目录 :  编辑 启动文件: 编辑 /usr/ ...

  6. react编译报错:Import in body of module; reorder to top import/first

    原因是这样的: 我把一个组件分为三个文件, |—Home.jsx // 业务逻辑与html Home——|—Home.css // 样式 |—index.js // 引入Home.css ,再把Hom ...

  7. luogu P3237 [HNOI2014]米特运输

    传送门 谢特运输 先要搞懂题目是什么意思,简化版题意就是一棵有根树,要使得每个点都满足任意一个儿子的权值等于这个点权值除以儿子数量,问最少要修改多少个点的点权 就可以一边dfs求出每个点权值是点1的多 ...

  8. 前端与后端的数据交互(jquery ajax+python flask)

    前端与后端的数据交互,最常用的就是GET.POST,比较常用的用法是:提交表单数据到后端,后端返回json 前端的数据发送与接收 1)提交表单数据 2)提交JSON数据 后端的数据接收与响应 1)接收 ...

  9. python的for循环、while循环

    1.for循环使用之乘法表 for i in range(1,10): for j in range(1,i+1): print('%s * %s = %s '%(j,i,i*j),end='') p ...

  10. java中的绝对路径和相对路径

    1.基本概念的理解 绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:/xyz/test.txt 代表了test.txt文件的绝对路径.http://www ...