Created
June 21, 2018 08:58
-
-
Save GerrieWell/482203380f20b203fa4aca60b1557c8a to your computer and use it in GitHub Desktop.
my python script for switching host
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import easydict | |
import yaml | |
import argparse | |
config = easydict.EasyDict() | |
config.users_host_path = '/Volumes/more/source/ilotuo/gerrieSwitchhost/user.yaml' | |
config.host_path = '/etc/hosts' | |
config.host_prefix = '##user-mark' | |
def main(): | |
parser = argparse.ArgumentParser(description='Process some integers.') | |
parser.add_argument('--disable', dest='disable', action='store_false') | |
args = parser.parse_args() | |
user_hosts = {} | |
with open(config.users_host_path,'r') as chf: | |
user_hosts = yaml.load(chf) | |
with open(config.host_path,"r+") as hf: | |
lines = hf.readlines() | |
i = len(lines) | |
for i,line in enumerate(lines): | |
if line.startswith(config.host_prefix): | |
lines = lines[:i+1] | |
break | |
hf.seek(0,0) | |
hf.writelines(lines) | |
hf.truncate() | |
if args.disable: | |
for key,value in user_hosts.items(): | |
hf.write('# ' + key+'\n') | |
hf.writelines(value) | |
if __name__ == '__main__': | |
main() | |
# | |
#yaml示例 | |
# | |
#user_host: |- | |
# 0.0.0.0 weibo.com | |
# 0.0.0.0 jandan.net | |
# 0.0.0.0 hupu.com | |
# 0.0.0.0 taobao.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment