Win7 The above requires the use of administrator privileges.
Copy Code code as follows:
#-*-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 < 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")