diff --git a/toolbox.py b/toolbox.py index 23bbbfb..10e5a87 100644 --- a/toolbox.py +++ b/toolbox.py @@ -764,4 +764,23 @@ def zip_folder(source_folder, dest_folder, zip_name): def gen_time_str(): import time - return time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) \ No newline at end of file + 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 \ No newline at end of file