Skip to content

Instantly share code, notes, and snippets.

@SamHDev
Last active November 2, 2019 23:24
Show Gist options
  • Save SamHDev/92098b53c31140b40a53f3f1f50e1ed7 to your computer and use it in GitHub Desktop.
Save SamHDev/92098b53c31140b40a53f3f1f50e1ed7 to your computer and use it in GitHub Desktop.
A Flask CORS Proxy
# Requires flask and requests
import flask
import requests
app = flask.Flask(__name__)
# ------- Actual Code -------
@app.route("/cors_proxy/<path:urls>", methods=["GET", "POST"])
def cors_proxy(urls):
if flask.request.method in ["GET", "POST"]:
r = requests.request(flask.request.method, urls)
f = flask.Response(r.content, status=r.status_code)
for key in r.headers:
f.headers.add(key, r.headers[key])
f.headers.add("Forwarded", "Server/" + urls)
return f
else:
return flask.abort(405) # 405: Method Not Allowed
# ------- End of Actual Code -------
app.run()
# Usage:
# /cors_proxy/http://example.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment