脚本内容:
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://HdhCmsTest360kb测试数据/kb/2_122.html"
req = urllib2.Request(url)
html = urllib2.urlopen(req).read()
head_ver = html.find(r' google hosts ')
ver_before = len(" google hosts ")
tail_ver = html.find(r' 更新')
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' ', ' ', 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!"
结果如下图:
查看更多关于Python脚本自动更新hosts实现免翻墙访问google的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did88088