用CherryPy服务favicon时出现错误500

2024-04-25 12:54:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我的CherryPy应用程序在服务favicon时开始抛出错误500(见下面的回溯)。起初只有少数几台计算机有问题,现在所有的计算机都有错误。我认为我没有改变Python应用程序上的任何东西,也许是计算机配置上的一些变化。

我无法调试错误,因为它无法在我的开发计算机上重现。在我的电脑上,它甚至没有到达生产服务器上崩溃的那条线。

以下是配置(其他参数由其他模块添加,这是处理favicon的初始配置):

config = {'global': {'server.socket_host':  '0.0.0.0',
                     'server.socket_port':  80,
                     'log.error_file':      'log\\web.log',
                     'log.access_file':     'log\\access.log'},
          '/favicon.ico': {'tools.staticfile.on':       True,
                           'tools.staticfile.filename': os.path.dirname(__file__) + '\\favicon.ico'}}

回溯如下:

^{pr2}$

Tags: 服务器log应用程序accessserver计算机错误socket
2条回答

尝试更改此行:

'tools.staticfile.filename': os.path.dirname(__file__) + '\\favicon.ico'

'tools.staticfile.filename': os.path.join(os.path.dirname(__file__), 'favicon.ico')

另一种方法是从html(在页眉部分)为favicon提供服务:

<link href="yourpath/favicon.ico" rel="icon" type="image/x-icon" />

misakm的回答是不正确的,但在调试过程中帮助了我:问题是__file__在开发计算机上是绝对的,在生产计算机上是相对的。我找到了原因here

If you load a module in the current directory, and the current directory isn't in sys.path, you'll get an absolute path.

If you load a module in the current directory, and the current directory is in sys.path, you'll get a relative path.

解决方案是这样添加abspath

os.path.join(os.path.dirname(os.path.abspath(__file__)), 'favicon.ico')

罪魁祸首是几周前对Windows环境进行的一些维护,它改变了路径。在

相关问题 更多 >