When Google is used to search for technical articles, it often fails to open the webpage from time to time. so I wrote a python script, and I feel that it is okay to use it. I would like to share it with you: windows 7 or WIn8 users must use the administrator permission to execute the command.
Project address: http://code.google.com/p/my-hosts-file/downloads
The code is as follows:
Import urllib
Import OS
Import shutil
Hostspath = "C: \ Windows \ System32 \ drivers \ etc"
Savepath = hostspath + "\ hostsave"
Def download_hosts (url = "http://my-hosts-file.googlecode.com/svn/trunk/hosts "):
OS. chdir (hostspath)
If OS. getcwd ()! = Hostspath:
Print ("Switch Dir to System32 Error, check permission! \ Npwd: "+ OS. getcwd ())
Exit ()
Try:
Urllib. urlretrieve (url, "hostsave ")
Except t:
Print '\ t Error when retrieveing hosts file from url:', url
Def backup_hosts ():
Shutil. copy ("hosts", "hosts. bak ")
Def replace_hosts ():
Shutil. copy ("hostsave", "hosts ")
Print ("Replace original hosts file finished, then flush dns ...")
OS. remove (savepath)
OS. system ("ipconfig/flushdns ")
Def main ():
Download_hosts ()
Backup_hosts ()
Replace_hosts ()
If _ name _ = '_ main __':
Main ()