Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
import requests
|
| 3 |
import logging
|
| 4 |
|
|
@@ -9,39 +9,45 @@ app = Flask(__name__)
|
|
| 9 |
def proxy_request(full_url):
|
| 10 |
try:
|
| 11 |
headers = {key: value for key, value in request.headers.items() if key.lower() != 'host'}
|
| 12 |
-
headers['User-Agent'] = request.headers.get('User-Agent', 'Mozilla/5.0 (compatible;
|
| 13 |
response = requests.request(
|
| 14 |
method=request.method,
|
| 15 |
-
url=full_url,
|
| 16 |
headers=headers,
|
| 17 |
data=request.get_data(),
|
| 18 |
params=request.args,
|
| 19 |
-
timeout=60,
|
| 20 |
-
|
| 21 |
)
|
| 22 |
-
return response.
|
| 23 |
except requests.exceptions.RequestException as e:
|
| 24 |
logging.error(f"代理请求错误: {e}")
|
| 25 |
return jsonify({"error": str(e)}), 500
|
| 26 |
|
| 27 |
@app.route('/proxy/<path:url>')
|
| 28 |
def handle_proxy(url):
|
| 29 |
-
# 确保url是完整的;如果不是,添加协议
|
| 30 |
if not url.startswith('http'):
|
| 31 |
full_url = f"https://{url}"
|
| 32 |
else:
|
| 33 |
-
full_url = url #
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
)
|
| 40 |
-
# 过滤并传递头部,避免冲突
|
| 41 |
for key, value in headers.items():
|
| 42 |
-
if key.lower() not in ['transfer-encoding', 'connection']: #
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
if __name__ == '__main__':
|
| 47 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, make_response
|
| 2 |
import requests
|
| 3 |
import logging
|
| 4 |
|
|
|
|
| 9 |
def proxy_request(full_url):
|
| 10 |
try:
|
| 11 |
headers = {key: value for key, value in request.headers.items() if key.lower() != 'host'}
|
| 12 |
+
headers['User-Agent'] = request.headers.get('User-Agent', 'Mozilla/5.0 (compatible; SpacesProxy/1.0)')
|
| 13 |
response = requests.request(
|
| 14 |
method=request.method,
|
| 15 |
+
url=full_url,
|
| 16 |
headers=headers,
|
| 17 |
data=request.get_data(),
|
| 18 |
params=request.args,
|
| 19 |
+
timeout=60,
|
| 20 |
+
stream=True # 启用流式以处理大响应,如HTML页面
|
| 21 |
)
|
| 22 |
+
return response.iter_content(chunk_size=1024), response.status_code, response.headers # 返回迭代器以流式输出
|
| 23 |
except requests.exceptions.RequestException as e:
|
| 24 |
logging.error(f"代理请求错误: {e}")
|
| 25 |
return jsonify({"error": str(e)}), 500
|
| 26 |
|
| 27 |
@app.route('/proxy/<path:url>')
|
| 28 |
def handle_proxy(url):
|
|
|
|
| 29 |
if not url.startswith('http'):
|
| 30 |
full_url = f"https://{url}"
|
| 31 |
else:
|
| 32 |
+
full_url = url # 使用传入的完整URL
|
| 33 |
+
|
| 34 |
+
content_iter, status_code, headers = proxy_request(full_url) # 获取迭代器
|
| 35 |
+
|
| 36 |
+
resp = make_response('') # 初始化响应
|
| 37 |
+
resp.status_code = status_code
|
|
|
|
|
|
|
| 38 |
for key, value in headers.items():
|
| 39 |
+
if key.lower() not in ['content-encoding', 'content-length', 'transfer-encoding', 'connection', 'host']: # 更严格过滤可能冲突的头部
|
| 40 |
+
resp.headers[key] = value
|
| 41 |
+
resp.mimetype = headers.get('Content-Type', 'text/html') # 设置正确的MIME类型
|
| 42 |
+
|
| 43 |
+
# 流式写入内容
|
| 44 |
+
def generate():
|
| 45 |
+
for chunk in content_iter: # 逐块输出内容
|
| 46 |
+
yield chunk
|
| 47 |
+
resp.response = generate() # 将生成器设置为响应体
|
| 48 |
+
resp.headers['Content-Length'] = headers.get('Content-Length', '0') # 如果可用,设置内容长度
|
| 49 |
+
|
| 50 |
+
return resp
|
| 51 |
|
| 52 |
if __name__ == '__main__':
|
| 53 |
app.run(host='0.0.0.0', port=7860, debug=True)
|