uwsgi返回空白输出

Rav*_*avi 6 python uwsgi

我在CentOS 6服务器上设置了nginx和uwsgi。当我将某些数据发布到Web服务器时,它会被正确处理,但不会返回任何输出。如果我在应用程序函数返回之前在应用程序之前打印生成的html,则HTML在控制台中显示正确,但是控制台上的下一行是:

[pid: 31650|app: 0|req: 2/2] <server ip> () {48 vars in 873 bytes} [Mon Sep 15 18:19:45 2014] POST / => generated 0 bytes in 10583 msecs (HTTP/1.1 200) 1 headers in 44 bytes (418 switches on core 0)
Run Code Online (Sandbox Code Playgroud)

我增加了套接字超时时间,但没有任何区别。

编辑:我为此提出了一个奇怪的解决方法。我的html被存储在变量“ html”中。我从以下位置更改了代码:

return [html] #This would not return any output even though 'print html' was fine
Run Code Online (Sandbox Code Playgroud)

至:

open('/tmp/ot.txt', 'w').write(html)
d = open('/tmp/ot.txt').read()
return [d] #This works!
Run Code Online (Sandbox Code Playgroud)

我宁愿不使用我的解决方法。任何想法,为什么这是可行的和原始的无效。Python版本是2.6.6

jco*_*ctx 6

您的问题很可能是uwsgi静默删除了所有unicode对象,仅显示字节字符串。这就是为什么当您写入文件并读回它时,它可以工作的原因,因为write()会将unicode静默转换为ascii编码(如果包含ord(x) > 127,则会引发异常),而read()返回字节。

在这种情况下,解决方案将是to return [html.encode('utf8')]或您正在使用的任何编码。

我之所以发现这一点,是因为我在调用markdown.markdown(text)任何.md文件,并将.html文件直接附加到输出中。仅显示HTML,而不显示markdown,该模块返回Unicode。