Created
January 20, 2013 14:26
-
-
Save iurisilvio/4579043 to your computer and use it in GitHub Desktop.
Using bottle.ResourceManager to serve static files with `bottle.static_file`
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
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) |
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
$ 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