ap / main.py
ykl45's picture
Update main.py
4eeec78 verified
raw
history blame contribute delete
No virus
1.79 kB
from flask import Flask, request, Response
import requests
app = Flask(__name__)
PROXY_TARGET = "https://chatpro.ai-pro.org"
@app.route('/')
def home():
global PROXY_TARGET
# 获取原始HTTP GET请求的参数
resp = requests.get(f"{PROXY_TARGET}{request.full_path}")
# 返回从目标网站获取的响应数据
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]
response = Response(resp.content, resp.status_code, headers)
return response
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def proxy(path):
global PROXY_TARGET
# 构造目标URL
url = f"{PROXY_TARGET}/{path}"
# 根据原始请求的方法来发送对应的请求到目标服务器
if request.method == 'GET':
resp = requests.get(url, stream=True)
elif request.method == 'POST':
resp = requests.post(url, json=request.json, stream=True)
elif request.method == 'PUT':
resp = requests.put(url, json=request.json, stream=True)
elif request.method == 'DELETE':
resp = requests.delete(url, 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]
def generate():
for chunk in resp.iter_content(chunk_size=8192):
yield chunk
return Response(generate(), resp.status_code, headers)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=True)