#coding:utf-8
'''''
下载google host
修改 系统hosts文件
'''
from requests.packages.urllib3 import request__author__ = 'halfopen'  from bs4 import BeautifulSoup
from PIL import Image
import urllib2
import sys
import ImageEnhance
import urllib
import cookielib
import re
import os
import platform
import httplib
import timeclass GetGoogleHosts:system_type = u"unknown"url = u"http://laod.cn/hosts/2015-google-hosts.html"new_hosts_url = ""cj = cookielib.CookieJar()opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))headers = {"User-agent": u"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1"}UserAgent  = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36'def __init__(self):# 设置 cookieself.system_type = platform.system()# 获取最新hostsdef get_new_host(self):request  = urllib2.Request(self.new_hosts_url)request.add_header('Accept', "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp")request.add_header('Accept-Encoding', "*")request.add_header('User-Agent', self.UserAgent)html = urllib2.urlopen(request).read()# print htmlf = open("hosts", 'wb')f.write(html)f.close()# 获取最新hosts urldef get_new_hosts_url(self):try:request = urllib2.Request(self.url)request.add_header('Accept', "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp")request.add_header('Accept-Encoding', "*")request.add_header('User-Agent', self.UserAgent)print u"------->[I]:download start"html = str(urllib2.urlopen(request).read())# print html# obj = re.match(r'http.*txt', "http://laod.cn/wp-content/uploads/2015/09/20150901-hosts.txt")obj = re.match(r'.*http://laod.cn/wp-content/uploads/.*-hosts.txt', html)if obj is not None:str1 = obj.group(0)self.new_hosts_url = str1[len(str1)-60:len(str1)]print u"------->[I]:get new_hosts_url\n\t"+self.new_hosts_urlelse:print u"------->[E]:new_hosts_url do not match"exit()except Exception, e:print eexit()finally:print u"------->[I]:download finished"# 替换hosts文件def update_hosts(self):print u"------->[I]:system platform\n\t"+platform.system()self.get_new_hosts_url()self.get_new_host()# filename = u"hosts.old."+str(time.time())if platform.system()== u"Windows":print u"------->[I]:move C:\Windows\System32\drivers\etc\hosts to C:\Windows\System32\drivers\etc\hosts.old"os.system(u'copy C:\Windows\System32\drivers\etc\hosts  C:\Windows\System32\drivers\etc\hosts.old')print u"------->[I]:copy hosts to C:\Windows\System32\drivers\etc\hosts.old "os.system(u'copy hosts C:\Windows\System32\drivers\etc\hosts.old')elif platform.system() == u"Linux":print u"------->[I]:move /etc/hosts to /etc/"os.system(u'cp /etc/hosts  /etc/hosts.old')print u"------->[I]:copy new host file to /etc/hosts "os.system(u'cp hosts /etc/hosts')else:passif __name__ == u'__main__':print u"------->"getGoogleHost = GetGoogleHosts()getGoogleHost.update_hosts()# getGoogleHost.update_hosts()

python 更新谷歌hosts相关推荐

  1. 【转】huhamhire-hosts自动更新Google Hosts文件教程

    huhamhire-hosts是一款可以同步更新下载Google(谷歌)Hosts文件的神器,有了他就再也不用逐个寻找测试可用的谷歌服务器IP.谷歌Hosts文件了.从此,度娘不再是你的唯一,你可以再 ...

  2. 2017年实时更新系统hosts文件(不能访问时才会更新)不要用于非法行为 只供内部测试...

    重要提醒 此方法不要外传 还有不要用于违法网络协议的非法活动··只用于自己在家测试用 不要用于网络FQ工作···不要做损害国家安全的行为···本blog对此不负法律责任····· 另外的blog可能网 ...

  3. python如何更新包_python如何更新包 python更新包代码示例

    python如何更新包?本篇文章小编给大家分享一下python更新包代码示例,代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看. Python安装新包,pip是 ...

  4. python更新后yum问题

    python更新后yum问题 How to switch between Python versions on Fedora Linux Currently, the default python v ...

  5. python更新数据库表的时间字段_python更新数据库中某个字段的数据(方法详解)

    连接数据库基本操作,我把每一步的操作是为什么给大家注释一下,老手自行快进. 请注意这是连接数据库操作,还不是更新. import pymysql #导包 #连接数据库 db = pymysql.con ...

  6. Python更新数据库,解决转译字符与TypeError: XX format: a number is required, not str报错

    在使用python更新数据库的时候出现了好多坑,一个个填完之后整理出了完备的代码: import MySQLdb#链接数据库的套路操作 db = MySQLdb.connect("local ...

  7. python更新到哪个版本了_将Python自带版本(2.6.6)升级到2.7.9

    将Python自带版本(2.6.6)升级到2.7.9 查看当前python版本:# pythonPython 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) [GC ...

  8. python调用谷歌地图api_python显示地图与谷歌地图

    我找到了一个 googlemaps软件包来从python访问谷歌地图,但没有找到任何演示以在python代码中显示结果. 例如: gmaps = googlemaps.Client(key='Add ...

  9. Python更新失败:SSL错误——Conda/Python

    Python更新失败--SSL错误 (1)是正常Python环境下的错误: 例如: Could not fetch URL https://pypi.tuna.tsinghua.edu.cn/simp ...

最新文章

  1. 24本实体书包邮免费送!
  2. python【力扣LeetCode算法题库】27-移除元素
  3. 后台开发经典书籍--Redis深度历险:核心原理和应用实践
  4. springboot切面返回值_SpringBoot实战15-Spring基础-AOP
  5. 数据结构与算法之二叉搜索树
  6. 每周工作量及时间统计
  7. android 表格控件点击事件,Android零基础入门|RecyclerView点击事件处理
  8. 哈希(不可逆)加密通用类库函数
  9. thinkphp路由配置 php7.0,Thinkphp url路由配置
  10. Sun JVM 内存管理、参数与调优、内存分配与回收策略、GC 日志配置使用详解
  11. python贪心算法几个经典例子_闲来无事整一下贪心算法 用python实现的
  12. CNC:CNC计算机数控系统技术之斯沃数控仿真软件简介、软件界面(顶部栏、中间工具栏、左边栏、CNC工作区、液晶屏工作区、系统控制面板)之详细攻略
  13. jsp实现购物车结算页面
  14. 【杂项】任务管理器的专用GPU内存和共享GPU内存
  15. 基于lis3dh的简易倾角仪c源码_轻松应对并发问题,简易的火车票售票系统,Newbe.Claptrap 框架用例,第一步 — 业务分析...
  16. Akka 指南 之「Actors」
  17. UE4学习之VR(三)
  18. 海量固定资产如何进行盘点?
  19. 【JAVA EE#6】【传智书城·源码阅读】后台管理模块:权限控制+页面分析+商品管理+销售榜单+订单管理+公告管理+项目结构思维导图
  20. c语言 库函数理解,C语言库函数理解

热门文章

  1. R之四格表与列联表数据的统计处理
  2. 机器视觉——入门基础(三)——相机镜头选型
  3. 【Linux Note】centos 7 / 8 通过修改网络配置文件 ifcfg-ens33来设置静态IP
  4. queen mary java_机器学习网站
  5. linux 编写shell脚本
  6. Steinberg Cubase 5 v5.1.2 For Windows 中文汉化完整版加精简版
  7. CCV所用资料资源汇总
  8. 人工神经网络 —— 神经元和感知机
  9. 从晏才宏老师的凄凉到李开复讲座之“火爆”
  10. 移动硬盘格式化恢复软件下载