You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
diff --git a/trunk/osx_linux.py b/trunk/osx_linux.py
index 35be490..2827978 100644
--- a/trunk/osx_linux.py+++ b/trunk/osx_linux.py@@ -8,20 +8,41 @@ import os
from shutil import copyfile
-HOSTS_URL='https://smarthosts.googlecode.com/svn/trunk/hosts'+HOSTS_URL='https://raw.githubusercontent.com/smarthosts/smarthosts/master/trunk/hosts'+# HOSTS_URL='https://smarthosts.googlecode.com/svn/trunk/hosts'
LOCAL_HOSTS='/etc/hosts'
+TAG_START = '###start smarthosts###'+TAG_END = '###end smarthosts###'
def main():
"""主函数"""
#备份文件
copyfile(LOCAL_HOSTS,'hosts.bak')
- with open(LOCAL_HOSTS,'aw') as hosts:- hosts.write(os.linesep)- #转义windows和unix的换行- for line in urllib.urlopen(HOSTS_URL):- hosts.write(line.strip()+os.linesep)++ lines = list()+ start = -1+ end = -1+ with open(LOCAL_HOSTS, 'r') as f:+ for i, line in enumerate(f):+ if line.startswith(TAG_START):+ start = i+ elif line.startswith(TAG_END):+ end = i+ line=''+ if end == -1 and start == -1:+ lines.append(line)+ elif end > -1:+ lines.append(line)+ with open(LOCAL_HOSTS,'w') as f:+ for line in lines:+ f.write(line)+ f.write(TAG_START+os.linesep)+ for line in urllib.urlopen(HOSTS_URL):+ f.write(line.strip()+os.linesep)+ f.write(TAG_END)+
print "success!"
if __name__ == '__main__':
The text was updated successfully, but these errors were encountered:
osx_linux.py脚本文件改进,每次只需要执行该脚本即可,无需对原hosts文件手动修改,解决问题如下:
场景需求: 假如我的hosts文件里配置了几个家庭主机的host信息(或者公司的host信息)例:
原来脚本执行之后,需要手动从hosts.bak文件恢复,新脚本将无需改动自动在后面追加云hosts配置信息,当再次更新时会自动查找并删除原云hosts配置信息,家庭或企业hosts信息将保留,无需手动修改
osx_linux.py脚本文件以及patch见最后面.
tag: googlecode地址能访问的几率远远低于github,故将云hosts的URL文件修改为github地址
非常感谢你们的项目,愿尽绵薄之力,谢谢!
另敬请对外提供donate页面
osx_linux.py脚本文件
patch文件内容
The text was updated successfully, but these errors were encountered: