A python script that solves the problem of Web pages when Google searches for technical articles _python

Source: Internet
Author: User
Tags python script
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 ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.