Proxy / app.py
Aleye's picture
Update app.py
9722d0e verified
from flask import Flask, request, Response
import requests
import os
app = Flask(__name__)
TARGET_DOMAIN = os.getenv("TARGET_API")
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def proxy(path):
try:
target_url = f"{TARGET_DOMAIN}/{path}"
#print(f"Proxying to: {target_url}")
headers = {key: value for (key, value) in request.headers if key.lower() not in ('host', 'content-length')}
headers['Accept-Encoding'] = 'identity' # 禁止压缩
resp = requests.request(
method=request.method,
url=target_url,
headers=headers,
data=request.get_data(),
cookies=request.cookies,
params=request.args,
stream=True
)
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
headers.append(('Content-Type', 'application/json; charset=utf-8'))
return Response(resp.iter_content(chunk_size=1024), headers=headers, status=resp.status_code)
except Exception as e:
return str(e), 500
if __name__ == '__main__':
# 必须指定host和port
app.run(host='0.0.0.0', port=7860, debug=False)