简体   繁体   中英

Not Found Error (404) with Static files when running from prompt

I recently moved from windows to raspberry pi for my app. It loaded at least once but now for the life of me I can't get static files to load again.

If I run the python script from shell as sudo (or without) I get 404 for all static files, dynamic links still work as expected.

If I run it from IDLE logging in as 'pi' it works fine.

Relevant code:

from bottle import route, run, get, request, static_file

    @get('/pumps')
    def pumpData():
        return json.dumps(pump.getPumps())

    # root dir
    @route('/<filename>')
    def server_static(filename):
        return static_file(filename, root='')

    # css dir
    @route('/css/<filename>')
    def server_static(filename):
        return static_file(filename, root='css')

    run(host='myip', port=2000, debug=True)

What could be causing the issue? I could guess its something to do with permissions but I dont know how I would fix it.

I don't think it's a permission problem. (That would return a 403.) It's most likely a path issue.

The good news is: fixing it should be straightforward. (Famous last words. ;) You should either

  1. specify absolute an path as the root param to static_file , or
  2. call os.chdir() into the static file root before you call bottle.run .

So, this:

return static_file(filename, root='/path/to/your/static/file/root')

or this:

os.chdir('/path/to/your/static/file/root')
run(host='myip', port=2000, debug=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM