Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
ladder1984 committed Nov 3, 2014
1 parent d4cfac6 commit 5115921
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
updateHosts ChangeLog
==============
1.2.0 2014/11/4
可以删除hosts文件中127.0.0.1屏蔽网站的记录

1.1.3 2014/10/28
代码优化

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ updateHosts
- **(已完成)**可选的hosts更新源
- **(已完成)**运行时不显示窗口
- **(已完成)**打包成exe文件,无需安装python即可使用
- **暂时关闭**可去除AdBlock部分,默认启用
- **已去除**可去除AdBlock部分
- **(已完成)** 用户可自定义hosts内容
- 待添加

Expand Down
4 changes: 4 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ source1 = https://raw.githubusercontent.com/zxdrive/imouto.host/master/imouto.ho
source2 = https://raw.githubusercontent.com/txthinking/google-hosts/master/hosts
source3 = https://smarthosts.googlecode.com/svn/trunk/hosts
source4 = https://simpleu.googlecode.com/svn/trunk/hosts

[other]
not_block_sites = 0
#0为保持hosts文件不变,1为关闭hosts中通过127.0.0.1对网站的屏蔽
16 changes: 12 additions & 4 deletions updateHosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#name:updateHosts
#author:https://github.com/ladder1984
#python version:2.7.8
#version:1.1.3
#version:1.2.0
############################

from urllib import urlretrieve
Expand All @@ -15,16 +15,19 @@
import ConfigParser

#default setting
hosts_source = "https://raw.githubusercontent.com/zxdrive/imouto.host/master/imouto.host.txt"
hosts_folder = os.environ['SYSTEMROOT']+"\\System32\\drivers\\etc\\"
hosts_location = hosts_folder + "hosts"

hosts_source = "https://raw.githubusercontent.com/zxdrive/imouto.host/master/imouto.host.txt"
not_block_sites = 0
#default setting

errorLog = open('errorLog.txt', 'a')


def get_config():
global hosts_source
global not_block_sites
if os.path.exists('config.ini'):
#清除Windows记事本自动添加的BOM
content = open('config.ini').read()
Expand All @@ -37,6 +40,7 @@ def get_config():
config.read('config.ini')
source_id = config.get('source_select', 'source_id')
hosts_source = config.get('source_select', 'source'+source_id)
not_block_sites = config.get("other", "not_block_sites")


def backup_hosts():
Expand All @@ -58,12 +62,16 @@ def process_hosts():
hosts_content = open('hosts','w')
file_from_web = open('hosts_from_web')
hosts_from_web = file_from_web.read()
file_user_defined=open('hosts_user_defined.txt')
hosts_user_defined=file_user_defined.read()
file_user_defined = open('hosts_user_defined.txt')
hosts_user_defined = file_user_defined.read()
hosts_content.write("127.0.0.1 localhost\n")
hosts_content.write("::1 localhost\n\n")
hosts_content.write('#hosts_user_defined\n')
hosts_content.write(hosts_user_defined)
hosts_content.write('\n#hosts_user_defined\n')
hosts_content.write('\n\n#hosts_by_hostsUpdate\n\n')
if not_block_sites is "1":
hosts_from_web = re.sub("127.0.0.1", "#not_block_sites", hosts_from_web)
hosts_content.write(hosts_from_web)
hosts_content.write('\n#hosts_by_hostsUpdate')
hosts_content.close()
Expand Down

0 comments on commit 5115921

Please sign in to comment.