Proxy a GET request to a different site in Python

狂风中的少年 提交于 2021-02-04 20:57:39

问题


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

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

Does anyone know how can it be done?


回答1:


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()

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()


来源:https://stackoverflow.com/questions/50222206/proxy-a-get-request-to-a-different-site-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!