Skip to content

Instantly share code, notes, and snippets.

@iurisilvio
Created January 20, 2013 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iurisilvio/4579043 to your computer and use it in GitHub Desktop.
Save iurisilvio/4579043 to your computer and use it in GitHub Desktop.
Using bottle.ResourceManager to serve static files with `bottle.static_file`
import os
import bottle
# consider root folder with:
# path1/file1.txt
# path2/file2.txt
# path3/file3.txt
rm = bottle.ResourceManager()
rm.add_path('path1/')
rm.add_path('path2/')
@bottle.route('/s/<filepath:path>')
def sr(filepath):
path_ = rm.lookup(filepath)
root, file_ = os.path.split(path_)
return bottle.static_file(file_, root)
if __name__ == '__main__':
bottle.debug(True)
bottle.run(reloader=True)
$ curl http://localhost:8080/s/file1.txt
file1
$ curl http://localhost:8080/s/file2.txt
file2
$ curl http://localhost:8080/s/file3.txt
[error]
$ curl http://localhost:8080/s/path3/file3.txt
[error]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment