A python script that solves the problem that Google cannot open web pages when searching technical articles

Source: Internet
Author: User
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 ()

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.