Windows XP Windows 7 Windows 2003 Windows Vista Windows教程綜合 Linux 系統教程
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟件教程
 Windows教程網 >> Linux系統教程 >> Linux教程 >> Google Hosts更新腳本

Google Hosts更新腳本

日期:2017/2/7 14:38:45      編輯:Linux教程
 

# -*- coding: utf-8 -*-

__author__ = 'xu.zhibin'
__createDateTime__ = '2012-06-25 11:54'

import os
import sys
import re
import urllib2

updateUrl="https://smarthosts.googlecode.com/svn/trunk/hosts"
hostPath="C:\WINDOWS\system32\drivers\etc\hosts"
pattern=re.compile(r"#Google Services START([\s|\S]*)#Google Services END")

#獲取最新的Google Hosts
try:
updateHost=urllib2.urlopen(updateUrl)
updateContent=updateHost.read()
except Exception,ex:
print "獲取最新的Google Hosts失敗!"
raw_input()
sys.exit()
match = pattern.search(updateContent)
if match != None:
googleContent=updateContent[match.start():match.end()]

#本地Hosts
hostFile=open(hostPath,'r+')
hostContent=hostFile.read()
bakContent = pattern.sub('',hostContent)
bakContent = re.sub("\n\n","\n",bakContent)
hostContent = googleContent+"\n"+bakContent
hostFile.seek(0)
hostFile.write(hostContent)
hostFile.close()

#Hosts 備份
bakPath="%s_bak" % hostPath
bakFile=open(bakPath,'w')
bakFile.write(bakContent)
bakFile.close()

print "更新Hosts成功!"
raw_input()

Copyright © Windows教程網 All Rights Reserved