解决谷歌搜索技术文章时打不开网页问题的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判断IP地址合法性的方法实例

使用Python判断IP地址合法性的方法实例

一、使用方法和执行效果请看图:二、python实现代码:复制代码 代码如下:[root@yang python]# vi check_ip.py #!/usr/bin/python im...

Python生成器(Generator)详解

通过列表生成式,我们可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后...

使用python实现strcmp函数功能示例

实现这个功能我相信大家一定明白他的意思了,很简单了,下面的代码大家参考使用吧复制代码 代码如下:def strcmp(str1,str2):    ...

利用python GDAL库读写geotiff格式的遥感影像方法

如下所示: from osgeo import gdal import numpy as np def read_tiff(inpath): ds=gdal.Open(inpat...

python 实现一次性在文件中写入多行的方法

将要写入的内容 构造 进一个list 中,使用writelines()方法 一次性写入。 file_w.writelines(list) file_w.flush() file.c...