python - bottle+gevent 不能提供静态文件

标签 python gevent bottle

我正在使用 bottlegevent对于我的 python webdev 实验。 我的问题是我无法提供静态文件,例如在我的模板中使用外部 CSS。 我的文件夹结构是:/static/css/style.css

我的代码:

索引.py

# -*- coding: UTF-8 -*-
from gevent import monkey; monkey.patch_all() #patching default Python threads
from bottle import mount, run, debug #initializing bottle
from routes import root #importing site routes
debug( True )
run( app = root , host = '0.0.0.0' , port = 80 , server = 'gevent' )

routes.py

# -*- coding: UTF-8 -*-
from bottle import *
root = Bottle()

@root.get('/static/<path:path>')
def serve_files( path ):
    return static_file( path , root = '/static/' )

这是我从终端回溯:

xxx.xxx.xxx.xxx - - [2011-12-22 09:36:44] "GET /static/css/style.css HTTP/1.1" 500 161 0.002867
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gevent-0.13.6-py2.7-linux-i686.egg/gevent/pywsgi.py", line 438, in handle_one_response
    self.run_application()
  File "/usr/local/lib/python2.7/dist-packages/gevent-0.13.6-py2.7-linux-i686.egg/gevent/pywsgi.py", line 424, in run_application
    self.result = self.application(self.environ, self.start_response)
  File "/usr/local/lib/python2.7/dist-packages/bottle-0.10.4-py2.7.egg/bottle.py", line 849, in __call__
    return self.wsgi(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/bottle-0.10.4-py2.7.egg/bottle.py", line 841, in wsgi
    % (html_escape(repr(_e())), html_escape(format_exc(10)))
NameError: global name '_e' is not defined

请帮忙。

更新:

我已经下载了不稳定版本的 Bottle(0.11 版)并将其导入到我的脚本中。现在没有 500 错误和回溯,但是 style.css 给了我 404。

[2011-12-22 12:42:59] "GET /static/css/style.css HTTP/1.1" 404 122 0.000591

最佳答案

你的 404 是因为你的静态文件的根路径可能是错误的。

root='/static/' 只有当您的根文件系统中有一个 static 文件夹时才可以。可能这不是你真正拥有的。如果您有一个项目文件夹,并且在此文件夹中有一个 static 文件夹,请使用 root='./static/' 它将正常工作。

关于python - bottle+gevent 不能提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8602563/

相关文章:

python - 关于 Monty Hall 问题的程序未返回预期结果

python - 如何启用请求异步模式?

python - 如何防止 Python Bottle 向控制台报告每个请求?

python - Bottle 模板

python - 使用 nginx 和 Python 防止网站在更新期间关闭

python - 需要从 Python Flask-Appbuilder 连接到 SQL Server 的帮助

python - Scrapy 集群 kafka_monitor.py 中断

具有多线程的 Python 请求

python - 为什么将 Gunicorn 与 GEvent 一起使用会增加对 Redis/数据库的查询时间?

python - 如何列出 networkx 中的特定节点/边缘?