`
nanjingjiangbiao_T
  • 浏览: 2593734 次
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

用谷歌搜索技术文章时打不开网页的的一个python脚本

 
阅读更多

发现用谷歌在搜索技术文章时,有时候会时不时的打不开网页,我写了一个脚本,感觉还好用,分享一下。

注意:Win7或者WIn8用户要用管理员权限执行

项目所在地址:http://code.google.com/p/my-hosts-file/downloads

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()


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics