欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解决谷歌搜索技术文章时打不开网页问题的python脚本

程序员文章站 2022-12-28 08:02:01
注意:win7或者win8用户要用管理员权限执行。 项目地址:复制代码 代码如下:import urllib      import...
注意:win7或者win8用户要用管理员权限执行。

项目地址:

复制代码 代码如下:

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