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

yipeiwu_com6年前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()

相关文章

python自动化报告的输出用例详解

python自动化报告的输出用例详解

1、设计简单的用例 2、设计用例    以TestBaiduLinks.py命名 # coding:utf-8 from selenium import webdriver imp...

Python字符串格式化

在许多编程语言中都包含有格式化字符串的功能,比如C和Fortran语言中的格式化输入输出。Python中内置有对字符串进行格式化的操作%。 模板 格式化字符串时,Python使用一个字符...

Python实现的RSS阅读器实例

本文实例讲述了Python实现的RSS阅读器。分享给大家供大家参考。具体如下: # -*- coding:utf-8 -*- # file: pyRSS.py # import Tk...

Python对数据库操作

Windows下安装MySQL-python 下载地址:https://pypi.python.org/pypi/MySQL-python/1.2.5 安装到系统即可。 linux下...

python实现智能语音天气预报

python实现智能语音天气预报

python编写的语音天气预报 本系统主要包括四个函数: 1、获取天气数据 1、输入要查询天气的城市 2、利用urllib模块向中华万年历天气api接口请求天气数据 3、利用gzip解压...