Python+DNS+MySQL
来源:互联网 发布:php webservice教程 编辑:程序博客网 时间:2024/06/09 14:49
Python+DNS+MySQL
具体任务和上一次一样,不过这次更细致具体
1.先在数据库中创建好Domain_info表格
打开数据库使用WJX数据库创建表
CREATE TABLE Domain_info(ID int(12) AUTO_INCREMENT,Domain char(20),A_record char(20),NS_record char(30),Domain_local char(10),Insert_time char(30),PRIMARY KEY(ID))ENGINE = InnoDB, CHARSET = 'UTF8';
这样一张表就创建好了
先在表中对自增变量插入数据
USE WJX;INSERT INTO Domain_info(ID)VALUES(1);
插入数据后是这样
下面用Python插入我们想要的数据(前面的程序完全可以在Python中完成)
我们要使用的模块
import DNSimport MySQLdbimport requestsimport reimport sysimport timefrom time import ctime
第一步,使用在主函数中通过读取文件获得的域名,通过DNS请求获得对应域名的A_record,也就是IP地址
def FindIp(Domain): query = sys.argv[0] DNS.DiscoverNameServers() reqobj = DNS.Request(Domain) answerobj = reqobj.req(name = query, qtype = DNS.Type.A) if not len(answerobj.answers): return for item in answerobj.answers: A_record = "%s" % item['data'] return A_record
同样的NS_reocrd也能通过同样的方式来获取、
def FindNS(Domain): query = sys.argv[0] DNS.DiscoverNameServers() reqobj = DNS.Request(Domain) answerobj = reqobj.req(name = query, qtype = DNS.Type.NS) if not len(answerobj.answers): return for item in answerobj.answers: NS_record = "%s" % item['data'] return NS_record
然后继续寻找Domain对应的地理位置
在这里我们使用requests.get()函数来爬取页面信息
我们知道ip138网站可以查询ip地理位置,并且其URL是
http://www.ip138.com/ips138.asp?ip=“要查询的ip地址”&action=2的模式,所以我们不难要得到我们想要的ip地理位置信息
def FindAdre(A_record): if A_record: try: adre_url= "http://www.ip138.com/ips138.asp?ip="+A_record+"&action=2" adre_get= requests.get(adre_url) adre_get.raise_for_status() adre_get.encoding = "gb2312" keyword = re.compile(r'''<td align="center"><ul class="ul1"><li>(.*?)</li><li>''', re.U|re.S) unic = keyword.findall(adre_get.text) Domain_local = ''.join(unic) return Domain_local[5:] except: print "error" else : return 0
通过爬取页面,我们得到了我们想要到地理位置信息
“本站数据:XXXX”
为了简化Domain_local,使用了字符串处理Domain_local[5:]来对地理位置信息简化,去掉了前面的“本站数据:”
接来下是向创建好的数据库插入数据
def MySQLSave(NS_record, Domain, A_record, Domain_local): db = MySQLdb.connect("XXX.XX.XXX.X","root","******","WJX",charset = "utf8") '''db = MySQLdb.connect("localhost","root","949501","WJX",charset = "utf8")''' cursor = db.cursor() cursor.execute('INSERT INTO Domain_info (Domain, A_record, Domain_local, NS_record, Insert_time) \ values ( %s, %s, %s, %s, %s)', [Domain, A_record, Domain_local, NS_record, ctime()]) db.commit() return
最后是主函数
def main(): start = time.time() a = open("URL.txt",'r') '''fo_0 = open("adre.txt",'w')''' '''fo_1 = open("ip.txt",'w') ''' num = 0 while (num < 4000): Domain = a.readline()[:-1]#去掉每一行最后的空格 num = num +1 A_record = FindIp(Domain) if A_record: NS_record = FindNS(Domain) Domain_local = FindAdre(A_record) '''fo_1.write(ip+'\n') ''' print "%-4d %-40s %-40s %-40s %-40s" % ( num, Domain, A_record, NS_record, Domain_local) , ctime() MySQLSave(NS_record, Domain, A_record, Domain_local) else: '''fo_1.write("NULL"+'\n')''' print "%-4d %-30s %-40s" % (num, Domain, "NotFound") a.close() '''fo_0.close()''' '''fo_1.close()''' end = time.time() print "TIME: ",end - start, "S"if __name__=='__main__': main()
这个程序还有待改进,单线程总是跑到很慢,而且db.commit()
存储的频率太快,降低了程序的运行速度。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
突然想到前几天有人问我DNS能不能刷浏览量
不可以
因为一旦DNS服务器接收到了你网站的IP地址,他给你返回IP的同时,他会对IP进行缓存,那么你下一次同样的请求他会直接从数据库获取这个映射,返回给你对应IP地址,而不是再一次访问目的网站。
刷浏览量还是通过爬虫或者其他方式吧。
路漫漫,共勉之。
- Python+DNS+MySQL
- python,DNS,MySQL,爬虫
- Python+MySQL+爬虫+DNS实现简单的综合性任务
- Python:DNS客户端实现
- python dns协议重写
- python实现DNS查询
- Python:DNS客户端实现
- Python学习:DNS解析
- python -- DNS处理模块
- MySQL DNS Details
- dns mysql mail
- [App] DNS Bind + MySQL
- [Python] DNS-Related Python Program
- [python网络编程]DNS服务器
- python - scapy - dns sniffer / posioning
- python DNS处理模块dnspython
- 《Python实现DNS解析查询》
- 使用Python进行DNS查询
- php单例模式,懒加载
- Java:异常
- 4.go开源cache2go项目笔记——cacheitem文件
- 数据传输-嵌入式
- Practise
- Python+DNS+MySQL
- 二.java多线程之线程状态转换
- 数据结构——堆栈的基本操作
- jmeter请求https地址
- 数据迁移
- Java中实现十进制数转换为二进制的几种办法
- ubuntu 下安装MATLAB2016出现install无法访问的权限问题
- DuplicateFileException: Duplicate files copied in APK
- 6.go开源cache2go项目笔记——cache_test文件