Skip to content

Instantly share code, notes, and snippets.

@fanzeyi
Created November 15, 2013 19:17
Show Gist options
  • Save fanzeyi/7490019 to your computer and use it in GitHub Desktop.
Save fanzeyi/7490019 to your computer and use it in GitHub Desktop.
Simple stream file proxy with Flask and Requests
# -*- coding: utf-8 -*-
from flask import Flask
from flask import Response
from flask import stream_with_context
import requests
app = Flask(__name__)
@app.route('/<path:url>')
def home(url):
req = requests.get(url, stream = True)
return Response(stream_with_context(req.iter_content()), content_type = req.headers['content-type'])
if __name__ == '__main__':
app.run()
@thavelick
Copy link

thavelick commented Jul 18, 2016

Note that iter_content() uses a chunk_size of 1 byte by default which makes proxying large files pretty slow. In some cases req.iter_content(chunk_size=1024) will work better.

@PostsDesert
Copy link

@thavelick Thanks, req.iter_content(chunk_size=1024) worked great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment