python 实现linux和docker下hosts切换

import time
import os
import sys



def merge_hosts():
def read_hosts(filename):
res = []
with open(filename, 'r') as f:
for line in f.readlines():
ls = line.split()
if len(ls) == 2:
res.append("{} {}".format(ls[0], ls[1]))
return res

# ./hosts是需要写入新的域名
ds = read_hosts('./hosts')
print(ds)
hosts = read_hosts('/etc/hosts')
ls = []
for s in ds:
if not s in hosts:
ls.append(s)
with open('/etc/hosts', 'a') as f:
for hosts in ls:
f.writelines(hosts)
f.write("\n")
f.close()



if __name__ == '__main__':
merge_hosts()
posted @ 2020-03-09 16:36  爱我所爱-333  阅读(301)  评论(0)    收藏  举报