您的位置:首页 > 编程语言 > Python开发

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

2018-10-12 13:50 776 查看
注意: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()

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: