+
36
-

通义千问的api服务器如何通过python进行代理?

通义千问的api服务器如何通过python进行代理?


网友回复

+
6
-

通过flask可以实现,具体代码如下:

import os
import sys
import threading
import webbrowser
import time
import requests
from urllib.parse import urljoin, quote
from flask import Flask, request, jsonify, send_from_directory, Response

app = Flask(__name__)

@app.route('/v1/<path:subpath>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'])
def proxy(subpath):
    # 定义目标URL
    target_url = urljoin('https://dashscope.aliyuncs.com/compatible-mode/v1/', quote(subpath)) 
    # 转发请求到目标URL
    headers = {key: value for (key, value) in request.headers if key != 'Host'}  # 移除原始 Host 头
    headers['Host'] = 'dashscope.aliyuncs.com'  # 修改为目标服务器的域名

    try:
        # 转发请求到目标URL
        response = requests.request(
        ...

点击查看剩余70%

我知道答案,我要回答