本文介绍了将GET请求代理到Python中的其他站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我从客户端收到的GET请求转发到另一个站点,

I want to forward a GET request that I get from a client to a different site,

以我为例-向流媒体网站发送的m3u8播放列表请求进行处理.

In my case- A m3u8 playlist request to a streaming site to handle.

有人知道怎么做吗?

推荐答案

如果要代理,请先安装 requests :

If you want to proxy, first install requests:

pip install requests

然后,在服务器中获取文件并提供内容,例如:

then, get the file in the server and serve the content, ej:

import requests
from flask import Flask, Response

app = Flask(__name__)

@app.route('/somefile.m3u')
def proxy():
    url = 'https://www.example.com/somefile.m3u'
    r = requests.get(url)
    return Response(r.content, mimetype="text/csv")

app.run()

如果您只想重定向,请执行此操作(不需要 requests ):

If you just want to redirect, do this (requests not needed):

from flask import Flask, redirect

app = Flask(__name__)

@app.route('/redir')
def redir():
    url = 'https://www.example.com/somefile.m3u'
    return redirect(url, code=302)

app.run()

这篇关于将GET请求代理到Python中的其他站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 12:34