python自动更新hosts

post by rocdk890 / 2015-1-30 9:29 Friday linux技术
  相信还是有很多人需要使用google来进行搜索,但国内这墙也封得越来越厉害了,连vpn都开始被封了,下面给大家个不用翻墙就可以访问google的脚本.
脚本内容:
vi google_update.py
#!/usr/bin/env python
# coding: utf-8
# Author : toddlerya
# Date: Jan 18 2015
import urllib2
import re
import sys
import os
url = "http://www.360kb.com/kb/2_122.html"
req = urllib2.Request(url)
html = urllib2.urlopen(req).read()
head_ver = html.find(r'<strong>google hosts&nbsp;</strong><strong>')
ver_before = len("<strong>google hosts&nbsp;</strong><strong>")
tail_ver = html.find(r'&nbsp;</strong>更新')

head_span = html.find('#base services')
tail_span = html.find('#google source end')
raw_hosts = html[head_span:tail_span]
result, number = re.subn(r'<.*>', '', raw_hosts)
pure_hosts, number = re.subn(r'&nbsp;', ' ', result)
arch = """127.0.0.1	localhost
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
"""
print "Update your host file start!"
#print "Please input your su password"
f = file(r'/tmp/hosts' ,'w+')
new_host = [arch,pure_hosts]
f.writelines(new_host)
f.close()
os.system('mv /tmp/hosts /etc/hosts')
print "Update success!"
结果如下图:
点击查看原图
ps:http://my.oschina.net/u/2003106/blog/369088
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-568.html

标签: update ip google python 自动更新

评论: