-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
33 lines (22 loc) · 974 Bytes
/
Copy pathapp.py
File metadata and controls
33 lines (22 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from flask import Flask, request, Response
import requests
app = Flask(__name__)
TARGET_URL = "https://openai.com"
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def proxy(path):
# 构建目标 URL
url = f"{TARGET_URL}/{path}"
# 获取请求方法
method = request.method
# 获取请求头和数据
headers = {key: value for key, value in request.headers if key != 'Host'}
data = request.get_data()
# 发起请求
response = requests.request(method, url, headers=headers, data=data, params=request.args)
# 构建响应
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
headers = [(name, value) for (name, value) in response.raw.headers.items() if name.lower() not in excluded_headers]
return Response(response.content, response.status_code, headers)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)