打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[Python网络编程] DNS缓存解决方案

http://blog.csdn.net/yueguanghaidao/article/details/26449911

2014

记得以前写爬虫的时候为了防止dns多次查询,是直接修改/etc/hosts文件的,最近看到一个优美的解决方案,修改后记录如下:

[python] view plain copy
  1. import socket  
  2.   
  3. _dnscache={}  
  4. def _setDNSCache():  
  5.     """ 
  6.     Makes a cached version of socket._getaddrinfo to avoid subsequent DNS requests. 
  7.     """  
  8.   
  9.     def _getaddrinfo(*args, **kwargs):  
  10.         global _dnscache  
  11.         if args in _dnscache:  
  12.             print str(args)+" in cache"  
  13.             return _dnscache[args]  
  14.   
  15.         else:  
  16.             print str(args)+" not in cache"    
  17.             _dnscache[args] = socket._getaddrinfo(*args, **kwargs)  
  18.             return _dnscache[args]  
  19.   
  20.     if not hasattr(socket, '_getaddrinfo'):  
  21.         socket._getaddrinfo = socket.getaddrinfo  
  22.         socket.getaddrinfo = _getaddrinfo  
  23.   
  24. def test():  
  25.     _setDNSCache()  
  26.     import urllib  
  27.     urllib.urlopen('http://www.baidu.com')  
  28.     urllib.urlopen('http://www.baidu.com')  
  29.   
  30. test()  

结果如下:

[python] view plain copy
  1. ('www.baidu.com', 80, 0, 1) not in cache  
  2. ('www.baidu.com', 80, 0, 1) in cache  

不过这个方案虽好,但也有缺陷,罗列如下:

1.相当于只对socket.getaddrinfo打了一个patch,但socket.gethostbyname,socket.gethostbyname_ex还是走之前的策略

2.只对本程序有效,而修改/etc/hosts将对所有程序有效,包括ping


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python爬虫爬取百度搜索结果
零基础自学用Python 3开发网络爬虫(三): 伪装浏览器君
分享一个简单的爬虫案例,几十行代码爬取百度贴吧,原理简单易懂
[经验]Python获取本机IP(外网IP)的方法总结
Python网页抓取urllib,urllib2,httplib[3]
《Python程序设计》第10章 网络编程
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服