Ofek Ezon
Ofek Ezon

Reputation: 39

Proxy a GET request to a different site in Python

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?

Upvotes: 2

Views: 3205

Answers (1)

AArias
AArias

Reputation: 2568

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

Upvotes: 5

Related Questions