Sign inSign up

jianbing/http_proxy

By jianbing

Updated over 2 years ago

Image
0

84

jianbing/http_proxy repository overview

代理任意请求,解决跨域问题

from flask import Flask, request, Response
import requests
from urllib.parse import unquote

app = Flask(__name__)

# 定义跨域清理函数,这里简单移除一些常见跨域头部
def clean_cors_headers(headers):
    cleaned_headers = {}
    for key, value in headers.items():
        if key.lower() not in ['access-control-allow-origin', 'access-control-allow-credentials', 'access-control-allow-methods', 'access-control-allow-headers']:
            cleaned_headers[key] = value
    return cleaned_headers

@app.route('/<path:target_url>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def proxy(target_url):
    # 解码URL
    target_url = unquote(target_url)

    # 获取请求的方法和数据
    method = request.method
    data = request.get_data()
    headers = {key: value for key, value in request.headers if key != 'Host'}

    # 请求目标URL
    response = requests.request(method, target_url, data=data)

    # 清理跨域内容
    content = response.content
    cleaned_headers = clean_cors_headers(response.headers)

    print(cleaned_headers)
    # 返回清理后的响应
    return Response(content, status=response.status_code, headers=cleaned_headers)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)

Tag summary

Content type

Image

Digest

sha256:78209798c

Size

51.3 MB

Last updated

over 2 years ago

docker pull jianbing/http_proxy