From ec8cc48a4de91e7584d4e77ea270070883a5cdc8 Mon Sep 17 00:00:00 2001 From: 505030475 <505030475@qq.com> Date: Thu, 25 May 2023 23:48:18 +0800 Subject: [PATCH] Add ProxyNetworkActivate --- toolbox.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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