Skip to content

Instantly share code, notes, and snippets.

@LtGlahn
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LtGlahn/f0cad0530b4b4298fa8c to your computer and use it in GitHub Desktop.
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.
# 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