Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, Response
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
|
8 |
+
TARGET_DOMAIN = os.getenv(TARGET_API)
|
9 |
+
|
10 |
+
@app.route('', defaults={'path' ''})
|
11 |
+
@app.route('pathpath', methods=['GET', 'POST', 'PUT', 'DELETE'])
|
12 |
+
def proxy(path)
|
13 |
+
try
|
14 |
+
target_url = f{TARGET_DOMAIN}{path}
|
15 |
+
#print(fProxying to {target_url})
|
16 |
+
headers = {key value for (key, value) in request.headers if key.lower() not in ('host', 'content-length')}
|
17 |
+
headers['Accept-Encoding'] = 'identity' # 禁止压缩
|
18 |
+
|
19 |
+
resp = requests.request(
|
20 |
+
method=request.method,
|
21 |
+
url=target_url,
|
22 |
+
headers=headers,
|
23 |
+
data=request.get_data(),
|
24 |
+
cookies=request.cookies,
|
25 |
+
params=request.args,
|
26 |
+
stream=True
|
27 |
+
)
|
28 |
+
|
29 |
+
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
30 |
+
headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
|
31 |
+
|
32 |
+
#headers.append(('Content-Type', 'applicationjson; charset=utf-8'))
|
33 |
+
|
34 |
+
return Response(resp.iter_content(chunk_size=1024), headers=headers, status=resp.status_code)
|
35 |
+
|
36 |
+
except Exception as e
|
37 |
+
return str(e), 500
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
if __name__ == '__main__'
|
43 |
+
# 必须指定host和port
|
44 |
+
app.run(host='0.0.0.0', port=7860, debug=False)
|