Last active
November 2, 2019 23:24
-
-
Save SamHDev/92098b53c31140b40a53f3f1f50e1ed7 to your computer and use it in GitHub Desktop.
A Flask CORS Proxy
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
# 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