Spaces:
Sleeping
Sleeping
Tuchuanhuhuhu
commited on
Commit
·
76e70d9
1
Parent(s):
b29abdd
新功能:检测环境变量中的HTTP(S)代理并使用
Browse files
utils.py
CHANGED
@@ -141,9 +141,38 @@ def get_response(
|
|
141 |
timeout = timeout_streaming
|
142 |
else:
|
143 |
timeout = timeout_all
|
144 |
-
|
145 |
-
|
146 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
return response
|
148 |
|
149 |
|
|
|
141 |
timeout = timeout_streaming
|
142 |
else:
|
143 |
timeout = timeout_all
|
144 |
+
|
145 |
+
# 获取环境变量中的代理设置
|
146 |
+
http_proxy = os.environ.get("HTTP_PROXY") or os.environ.get("http_proxy")
|
147 |
+
https_proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")
|
148 |
+
|
149 |
+
# 如果存在代理设置,使用它们
|
150 |
+
proxies = {}
|
151 |
+
if http_proxy:
|
152 |
+
logging.info(f"Using HTTP proxy: {http_proxy}")
|
153 |
+
proxies["http"] = http_proxy
|
154 |
+
if https_proxy:
|
155 |
+
logging.info(f"Using HTTPS proxy: {https_proxy}")
|
156 |
+
proxies["https"] = https_proxy
|
157 |
+
|
158 |
+
# 如果有代理,使用代理发送请求,否则使用默认设置发送请求
|
159 |
+
if proxies:
|
160 |
+
response = requests.post(
|
161 |
+
API_URL,
|
162 |
+
headers=headers,
|
163 |
+
json=payload,
|
164 |
+
stream=True,
|
165 |
+
timeout=timeout,
|
166 |
+
proxies=proxies,
|
167 |
+
)
|
168 |
+
else:
|
169 |
+
response = requests.post(
|
170 |
+
API_URL,
|
171 |
+
headers=headers,
|
172 |
+
json=payload,
|
173 |
+
stream=True,
|
174 |
+
timeout=timeout,
|
175 |
+
)
|
176 |
return response
|
177 |
|
178 |
|