• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python切换hosts文件代码示例

python切换hosts文件代码示例

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python allowed hosts,python hosts,hosts文件代码,hosts修改代码,hosts代码等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

win7以上需要使用管理员权限操作。

def format_file_list(files):
        all_files_str = ""
        for i in range(len(files)):
                all_files_str +=  str(i)+":"+files[i]+"\n"
        return all_files_str

hosts_path = "C:\\Windows\\System32\\drivers\\etc"
files =  os.listdir(hosts_path)
os.chdir(hosts_path)

if os.getcwd() != hosts_path:
        print("Switch Dir to System32 Error,check permission!\npwd:"+os.getcwd())
        exit()

hosts_files = glob.glob("host*")
choosed_file_idx = int(input("Choose Hosts File Index:\n"+format_file_list(hosts_files)))
files_num = len(hosts_files)

if (choosed_file_idx < 0 or choosed_file_idx >= files_num) :
        print("Please choose a file in the lists!")
        exit()

print("Choosed idx:{0},file:{1}.".format(choosed_file_idx,hosts_files[choosed_file_idx]))
shutil.copy("hosts","hosts.bak")
shutil.copy(hosts_files[choosed_file_idx],"hosts")
print("Copy ok,then flush dns...")
os.system("ipconfig /flushdns")
</div>

</div>

您可能想查找下面的文章:

  • python切换hosts文件代码示例

相关文章

  • 浅谈Python的文件类型
  • 在Python的Django框架中为代码添加注释的方法
  • Python实现冒泡,插入,选择排序简单实例
  • 简单介绍Python2.x版本中的cmp()方法的使用
  • python根据文件大小打log日志
  • python Django连接MySQL数据库做增删改查
  • python sort、sorted高级排序技巧
  • Python SQLAlchemy基本操作和常用技巧(包含大量实例,非常好)
  • Pthon批量处理将pdb文件生成dssp文件
  • python3中int(整型)的使用教程

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python抓取网页中的图片示例
    • Python设计模式之代理模式实例
    • python定时检查启动某个exe程序适合检测exe是否挂了
    • python抓取网页图片示例(python爬虫)
    • python压缩文件夹内所有文件为zip文件的方法
    • Python格式化css文件的方法
    • Python编程中归并排序算法的实现步骤详解
    • 总结Python编程中三条常用的技巧
    • Python 的 with 语句详解
    • Python中动态创建类实例的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有