工作笔记--Python自动切换host

修改host代码:

#coding:utf-8
import os,time

pwd = os.path.dirname(__file__) #获取当前文件夹的绝对路径
pull_host_cmd = 'adb pull system/etc/hosts '+pwd
push_host_cmd = 'adb push '+pwd+'\hosts system/etc/'

os.popen('adb root')
temp = os.popen('adb remount')

print 'Pull hosts..'
temp = os.popen(pull_host_cmd)
temp = temp.readline().split()

#读取配置文件
config_path = os.path.join(pwd, 'config.txt') #获取当前文件夹内的config文件
config_file = open(config_path, "r") #读取文件

#写追加方式打开host文件
host_path = os.path.join(pwd,'hosts')
host_file = open(host_path,"a+")

for line in config_file:
host_file.write(line)

config_file.close() #关闭文件
host_file.close()

#push hosts
temp = os.popen(push_host_cmd)
temp = temp.readline().split()
temp = os.system('del hosts')

重置到localhost的代码:
#coding:utf-8
import os,time

pwd = os.path.dirname(__file__) #获取当前文件夹的绝对路径
pull_host_cmd = 'adb pull system/etc/hosts '+pwd
push_host_cmd = 'adb push '+pwd+'\hosts system/etc/'

temp = os.popen(pull_host_cmd)
temp = temp.readline().split()

host_path = os.path.join(pwd,'hosts')
host_file = open(host_path,"wb")

host_file.write('127.0.0.1 localhost\n')
host_file.close()
os.system('adb push hosts system/etc/hosts')
os.system('del hosts')

Project目录:

 工作笔记--Python自动切换host_第1张图片

 

 

 

你可能感兴趣的:(工作笔记--Python自动切换host)