zoukankan      html  css  js  c++  java
  • 工作笔记--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 ')
    host_file.close()
    os.system('adb push hosts system/etc/hosts')
    os.system('del hosts')

    Project目录:

     

    
    
  • 相关阅读:
    shell脚本之for循环
    shell脚本小集锦
    Java构建指定大小文件
    IntelliJ+Maven+Spring+Tomcat项目搭建(MAC)
    Git下基本命令操作
    Mac下IntelliJ的Git、GitHub配置及使用
    Git下的.DS_Store文件
    Mac下GitHub以及GitHub Desktop使用实战
    idea快捷键
    汉字获取首字符
  • 原文地址:https://www.cnblogs.com/ailiailan/p/7919470.html
Copyright © 2011-2022 走看看