SoFunction
Updated on 2024-11-19

Python script to solve the problem of not being able to open a web page when searching Google for technical articles


import urllib 
    import os 
    import shutil 

    hostspath = "C:\\Windows\\System32\\drivers\\etc" 
    savepath = hostspath + "\\hostsave" 

    def download_hosts(url = "/svn/trunk/hosts"): 
        (hostspath) 
        if () != hostspath: 
            print("Switch Dir to System32 Error,check permission!\npwd:"+()) 
            exit()  
        try: 
            (url, "hostsave") 
        except: 
            print '\t Error when retrieveing hosts file from url: ', url 

    def backup_hosts(): 
        ("hosts","") 

    def replace_hosts(): 
        ("hostsave", "hosts") 
        print("Replace original hosts file finished, then flush dns...") 
        (savepath)     
        ("ipconfig /flushdns") 

    def main(): 
        download_hosts() 
        backup_hosts() 
        replace_hosts() 
    if __name__ == '__main__': 
        main()