search
HomeBackend DevelopmentPython Tutorialpython切换hosts文件代码示例

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

复制代码 代码如下:

# -*- coding: utf-8 -*-
import os
import glob
import shutil

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 = 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")

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Go语言编程指南:修改hosts文件的实现Go语言编程指南:修改hosts文件的实现Mar 07, 2024 am 10:51 AM

标题:Go语言编程指南:修改hosts文件的实现在日常的软件开发过程中,经常会遇到需要修改操作系统中hosts文件的情况。Hosts文件是一个没有扩展名的用于映射IP地址和主机名的文本文件,通常用于在本地设置域名解析。在本文中,将介绍如何使用Go语言来实现修改操作系统hosts文件的功能,并给出具体的代码示例。首先,我们需要创建一个可以修改hosts文件的G

hosts文件修改后无法保存hosts文件修改后无法保存Feb 20, 2024 am 08:14 AM

hosts文件是一种用来映射IP地址和主机域名的文本文件,用于解决域名解析问题。在某些情况下,我们可能需要对hosts文件进行修改,添加或删除一些条目。然而,有时候我们可能会遇到hosts文件修改后无法保存的问题。本文将探讨这个问题可能出现的原因,并提供一些解决方法。首先,hosts文件位于操作系统的系统目录中,如Windows操作系统下的路径是C:Wind

修改hosts文件无权限修改hosts文件无权限Mar 03, 2024 am 10:31 AM

有用户请求修改Hosts文件,但在保存时遭遇到无法保存的困扰,这是由于Hosts文件为系统文件,通常需获得相应权限方可编辑,因此如果你也遇到了下面的问题那么可以参考文章的方法来解决修改hosts文件无权限1、打开hosts文件然后把前面的只读给取消勾选2、如果是打开显示没有权限的话,那么就右键该文件然后选择属性3、点击安全进入4、随后再进入高级5、请在白框内录入“administrator”,然后点击确定。

win10找不到hosts文件怎么办win10找不到hosts文件怎么办Mar 01, 2024 pm 09:26 PM

win10系统中的hosts文件非常重要,与屏蔽网址、网页跳转等功能息息相关,因此用户也会经常更改文件设置,由于该文件默认是被隐藏起来的,因此很多用户找不到,接下来就帮助大家解决问题。win10找不到hosts文件怎么办1、首先点击此电脑,通过Windows-Systen32-Drivers-etc路径进入文件夹,点击界面上方的查看,在点击选项按钮。2、在弹出的窗口中点击上方的查看选项卡,找打高级设置模块中的隐藏受保护的操作系统文件。3、需要取消隐藏受保护的操作系统文件的勾选,在系统弹出的提示中

如何配置HOSTS文件如何配置HOSTS文件Jan 09, 2024 pm 03:41 PM

配置步骤:1、打开终端或命令提示符窗口,输入“cd /etc”命令,进入“/etc”文件夹下;2、输入“vi /hosts”命令,进入hosts文件的编辑界面;3、在编辑界面中,按下“i”键进入插入模式,然后手动输入或粘贴需要添加或修改的内容;4、完成编辑后,按下“Esc”键退出插入模式;5、在命令行中输入“:wq”命令并按下回车键,保存并退出编辑器;6、进行备份即可。

hosts文件怎么打开hosts文件怎么打开Nov 06, 2023 am 10:27 AM

hosts文件打开步骤:1、打开文件资源管理器;2、在地址栏中输入以下路径:C:\Windows\System32\drivers\etc\;3、在该路径下,即可找到一个名为“hosts”的文件;4、打开hosts文件即可。

Win8系统hosts文件不能修改怎么办Win8系统hosts文件不能修改怎么办Jun 29, 2023 pm 12:34 PM

Win8系统hosts文件不能修改怎么办?hosts文件是系统盘中的一个重要文件,如果我们需要修改ip地址,就需要修改这个文件,但是有小伙伴反应说hosts文件不能修改了,这种情况我们应该如何解决呢。小编下面整理了Win8系统hosts文件不能修改更改教程,感兴趣的话,跟着小编一起往下看看吧!Win8系统hosts文件不能修改更改教程方法一:使用管理员权限打开“记事本”来进行hosts文件编辑如果出现提示用户账户控制,我们需要点击“是”。在打开的记事本程序中打开C:WINDOWSsystem32

win10系统修改hosts文件的操作方法win10系统修改hosts文件的操作方法Mar 27, 2024 am 11:11 AM

1、打开设备,进入C盘,定位致C:windowssystem32driversetc;如图所示:2、接着,在文件夹中找到hosts文件,右键单击选取【属性】;如图所示:3、在hosts文件属性窗口内的【安全】一栏中找到正在使用的账号,给予【修改】和【写入】权限后,点击【确定】;如图所示:4、然后,以记事本格式打开hosts文件;如图所示:5、最后,输入一个指定IP访问,并进行保存就可以了。如图所示:

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.