Add ProxyNetworkActivate
This commit is contained in:
		
							parent
							
								
									5d75c578b9
								
							
						
					
					
						commit
						ec8cc48a4d
					
				
							
								
								
									
										19
									
								
								toolbox.py
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								toolbox.py
									
									
									
									
									
								
							@ -765,3 +765,22 @@ def zip_folder(source_folder, dest_folder, zip_name):
 | 
				
			|||||||
def gen_time_str():
 | 
					def gen_time_str():
 | 
				
			||||||
    import time
 | 
					    import time
 | 
				
			||||||
    return time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
 | 
					    return time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class ProxyNetworkActivate():
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    这段代码定义了一个名为TempProxy的空上下文管理器, 用于给一小段代码上代理
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    def __enter__(self):
 | 
				
			||||||
 | 
					        from toolbox import get_conf
 | 
				
			||||||
 | 
					        proxies, = get_conf('proxies')
 | 
				
			||||||
 | 
					        if 'no_proxy' in os.environ: os.environ.pop('no_proxy')
 | 
				
			||||||
 | 
					        os.environ['HTTP_PROXY'] = proxies['http']
 | 
				
			||||||
 | 
					        os.environ['HTTPS_PROXY'] = proxies['https']
 | 
				
			||||||
 | 
					        return self
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __exit__(self, exc_type, exc_value, traceback):
 | 
				
			||||||
 | 
					        os.environ['no_proxy'] = '*'
 | 
				
			||||||
 | 
					        if 'HTTP_PROXY' in os.environ: os.environ.pop('HTTP_PROXY')
 | 
				
			||||||
 | 
					        if 'HTTPS_PROXY' in os.environ: os.environ.pop('HTTPS_PROXY')
 | 
				
			||||||
 | 
					        return
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user