Last active
November 20, 2015 22:43
-
-
Save saml/82778e6170988cc6017e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import tarfile | |
import os | |
import requests | |
from flask import Flask, request, send_file | |
app = Flask(__name__) | |
def create_tarfile(urls): | |
tar_stream = io.BytesIO() | |
with tarfile.open(mode='w', fileobj=tar_stream) as tar: | |
for url in urls: | |
r = requests.get(url) | |
with io.BytesIO(r.content) as f: | |
name = os.path.basename(url) | |
info = tarfile.TarInfo(name) | |
info.size = len(r.content) | |
tar.addfile(info, f) | |
tar_stream.seek(0) | |
return tar_stream | |
@app.route('/') | |
def index(): | |
urls = request.args.getlist('url') | |
f = create_tarfile(urls) # f is intentionally not closed. Would flask close this? | |
return send_file(f, attachment_filename='foobar.tar') | |
if __name__ == '__main__': | |
app.run(port=5001, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment