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

yipeiwu_com5年前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实现的ini文件操作类分享

类代码: # -*- coding:gbk -*- import ConfigParser, os class INIFILE: def __init__(self, filen...

python3利用ctypes传入一个字符串类型的列表方法

c语言里:c_p.c #include <stdio.h> void get_str_list(int n, char *b[2]) { printf("in c s...

Python操作word常见方法示例【win32com与docx模块】

本文实例讲述了Python操作word常见方法。分享给大家供大家参考,具体如下: 这里介绍两种方式: 使用win32com 使用docx 1. 使用win32com扩展包 只...

对python .txt文件读取及数据处理方法总结

对python .txt文件读取及数据处理方法总结

1、处理包含数据的文件 最近利用Python读取txt文件时遇到了一个小问题,就是在计算两个np.narray()类型的数组时,出现了以下错误: TypeError: ufunc '...

基于Python实现一个简单的银行转账操作

基于Python实现一个简单的银行转账操作

前言 在进行一个应用系统的开发过程中,从上到下一般需要四个构件:客户端-业务逻辑层-数据访问层-数据库,其中数据访问层是一个底层、核心的技术。而且在实际开发中,数据库的操作也就是说数据访...