Created
August 19, 2020 12:13
-
-
Save raspberrypisig/a80662ab221e2e12ea3792d1a4a63665 to your computer and use it in GitHub Desktop.
This file contains 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
from flask import Flask, request, Response | |
import requests | |
PROXY_URL = 'http://ipv4.download.thinkbroadband.com/' | |
def download_file(streamable): | |
with streamable as stream: | |
stream.raise_for_status() | |
for chunk in stream.iter_content(chunk_size=8192): | |
yield chunk | |
def _proxy(*args, **kwargs): | |
resp = requests.request( | |
method=request.method, | |
url=request.url.replace(request.host_url, PROXY_URL), | |
headers={key: value for (key, value) in request.headers if key != 'Host'}, | |
data=request.get_data(), | |
cookies=request.cookies, | |
allow_redirects=False, | |
stream=True) | |
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection'] | |
headers = [(name, value) for (name, value) in resp.raw.headers.items() | |
if name.lower() not in excluded_headers] | |
return Response(download_file(resp), resp.status_code, headers) | |
app = Flask(__name__) | |
@app.route('/', defaults={'path': ''}) | |
@app.route('/<path:path>') | |
def download(path): | |
return _proxy() | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=1234, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment