python+flask实现一个反向代理https服务代码

代码语言:python

所属分类:其他

代码描述:python+flask实现一个反向代理https服务代码

代码标签: python flask 反向 代理 https 服务 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

#!/usr/local/python3/bin/python3
# -*- coding: utf-8 -*-
from flask import Flask, request, Response
import requests
from urllib.parse import urljoin, quote

app = Flask(__name__)

TARGET_URL = 'https://www.baidu.com'

@app.route('/', defaults={'path': ''}, methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'])
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'])
def proxy(path):
    # 使用 urljoin 来正确处理路径
    url = urljoin(TARGET_URL, quote(path))
    
    # 过滤和修改请求头
    headers = {}
    for key, value in request.headers:
        if key.lower() not in ['host', 'content-length']:
            headers[key] = value
    
    # 添加一些可能需要的头信息
    headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    
    try:
        .........完整代码请登录后点击上方下载按钮下载查看

网友评论0