简体   繁体   English

从提示符运行时,找不到静态文件的错误(404)

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

I recently moved from windows to raspberry pi for my app. 我最近从Windows移动到覆盆子pi为我的应用程序。 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. 如果我从shell运行python脚本作为sudo(或没有)我得到404所有静态文件,动态链接仍然按预期工作。

If I run it from IDLE logging in as 'pi' it works fine. 如果我从IDLE运行它作为'pi'登录它可以正常工作。

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. (这将返回403.)这很可能是路径问题。

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 指定绝对路径作为static_fileroot参数,或
  2. call os.chdir() into the static file root before you call bottle.run . 调用os.chdir()到静态文件根打电话之前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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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