Note: Win7 or WIN8 users will perform with administrator privileges.
Project Address: Http://code.google.com/p/my-hosts-file/downloads
Copy Code code 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
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 ()