ykl45 commited on
Commit
c263ea8
1 Parent(s): b94d4b1

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +33 -38
main.py CHANGED
@@ -1,48 +1,43 @@
1
- from flask import Flask, request, Response, stream_with_context
2
  import requests
3
 
4
  app = Flask(__name__)
5
 
6
- # 你想代理的网址
7
- PROXY_TARGET = "https://chatpro.ai-pro.org"
8
 
9
-
10
- @app.route('/', defaults={'path': ''})
11
- @app.route('/<path:path>', methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
 
 
 
 
 
 
 
 
 
 
12
  def proxy(path):
13
  global PROXY_TARGET
14
-
15
- # 将原始请求的头部信息转发到目标服务器
16
- headers = dict(request.headers)
 
 
 
 
 
 
 
 
17
 
18
- # 移除掉 Flask/Werkzeug 会默认修改的Headers
19
- headers.pop('Host', None)
20
- headers.pop('Content-Length', None)
21
-
22
- # 使用 requests 发送经过构造的请求到目标服务器
23
- resp = requests.request(
24
- method=request.method,
25
- url=f"{PROXY_TARGET}/{path}",
26
- headers=headers,
27
- data=request.get_data(),
28
- cookies=request.cookies,
29
- allow_redirects=False,
30
- stream=True
31
- )
32
-
33
- # 创建一个生成器, 按块读取响应内容, 不需要一次性全部读入内存
34
- def generate():
35
- for chunk in resp.iter_content(chunk_size=4096):
36
- yield chunk
37
-
38
- # 将目标服务器的响应头转发回客户端
39
- response_headers = [(name, value) for (name, value) in resp.raw.headers.items()]
40
-
41
- # 使用 stream_with_context 包装响应内容生成器
42
- response_stream = Response(stream_with_context(generate()), resp.status_code, response_headers)
43
-
44
- return response_stream
45
-
46
 
47
  if __name__ == '__main__':
48
- app.run(debug=True,host='0.0.0.0',port=7860)
 
1
+ from flask import Flask, request, Response
2
  import requests
3
 
4
  app = Flask(__name__)
5
 
6
+ PROXY_TARGET = "https://chatpro.ai-pro.org/chat"
 
7
 
8
+ @app.route('/')
9
+ def home():
10
+ global PROXY_TARGET
11
+ # 获取原始HTTP GET请求的参数
12
+ resp = requests.get(f"{PROXY_TARGET}{request.full_path}")
13
+ # 返回从目标网站获取的响应数据
14
+ excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
15
+ headers = [(name, value) for (name, value) in resp.raw.headers.items()
16
+ if name.lower() not in excluded_headers]
17
+ response = Response(resp.content, resp.status_code, headers)
18
+ return response
19
+
20
+ @app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
21
  def proxy(path):
22
  global PROXY_TARGET
23
+ # 构造目标URL
24
+ url = f"{PROXY_TARGET}/{path}"
25
+ # 根据原始请求的方法来发送对应的请求到目标服务器
26
+ if request.method == 'GET':
27
+ resp = requests.get(url, stream=True)
28
+ elif request.method == 'POST':
29
+ resp = requests.post(url, json=request.json, stream=True)
30
+ elif request.method == 'PUT':
31
+ resp = requests.put(url, json=request.json, stream=True)
32
+ elif request.method == 'DELETE':
33
+ resp = requests.delete(url, stream=True)
34
 
35
+ # 将从目标网站获取的响应返回给客户端
36
+ excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
37
+ headers = [(name, value) for (name, value) in resp.raw.headers.items()
38
+ if name.lower() not in excluded_headers]
39
+ response = Response(resp.content, resp.status_code , headers)
40
+ return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  if __name__ == '__main__':
43
+ app.run(debug=True)