-
-
Save LtGlahn/f0cad0530b4b4298fa8c to your computer and use it in GitHub Desktop.
Simple snippet to serve files from your #Flask #python webapplication with custom http-headers. For example adding a "accept-origin" header so that CORS (cross domain origin) requests will go through from any domain. Note that this is a rather crude (but effective!) treatment of CORS.
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
# The code snippet goes into your flask app | |
# Directory where we keep the data we want to serve | |
# Note trailing slash | |
dataDir = '/home/what/ever/where/ever/you/keep/your/files/' | |
@app.route('/getfile/<filename>') | |
def getFile(filename): | |
filename = dataDir + filename | |
try: | |
with open( filename) as file: | |
blob = file.read() | |
except: | |
abort(404) | |
pass | |
r = make_response(blob) | |
r.mimetype='application/json' | |
r.headers['Access-Control-Allow-Origin'] = '*' | |
return r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment