Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Django中,如何允许print语句与ApacheWSGi一起工作?_Python_Django_Apache_Debugging_Wsgi - Fatal编程技术网

Python 在Django中,如何允许print语句与ApacheWSGi一起工作?

Python 在Django中,如何允许print语句与ApacheWSGi一起工作?,python,django,apache,debugging,wsgi,Python,Django,Apache,Debugging,Wsgi,“打印”仅适用于开发服务器。 但是如果我想让它在Apache中工作呢?以防我忘了注释它…我希望能够顺利进行而不会导致错误 (只需打印为空)你的建议是个坏主意,但如果你坚持要这样做,请查看: WSGIRestrictStdout Description: Enable restrictions on use of STDOUT. Syntax: WSGIRestrictStdout On|Off Default: WSGIRestrictStdout On Context:

“打印”仅适用于开发服务器。 但是如果我想让它在Apache中工作呢?以防我忘了注释它…我希望能够顺利进行而不会导致错误


(只需打印为空)

你的建议是个坏主意,但如果你坚持要这样做,请查看:

WSGIRestrictStdout

Description: Enable restrictions on use of STDOUT.
Syntax:      WSGIRestrictStdout On|Off
Default:     WSGIRestrictStdout On
Context:     server config
Module:      mod_wsgi.c
一个性能良好的Python WSGI应用程序 不应尝试写入任何数据 直接转到sys.stdout或使用 不直接打印语句 指向备用文件对象。这是 因为托管WSGI的方式 CGI等应用程序使用标准 输出作为发送的机制 将响应的内容返回到 网络服务器。如果WSGI应用程序 直接写入sys.stdout 可能会干扰 WSGI适配器并导致 输出流的损坏

为了促进 WSGI应用程序的可移植性, mod_wsgi限制访问 sys.stdout并将引发异常 如果试图使用 sys.stdout显式

唯一一次你可能想 取消这一限制完全是不可能的 能够使用的方便性 调试期间打印语句 应用程序的,或者如果第三方 参与方模块或WSGI应用程序被删除 使用打印时出错 不应该。如果限制使用 sys.stdout被删除,任何数据 将改为发送书面通知 至sys.stderr,并将显示 在Apache错误日志文件中


见格雷厄姆·邓普尔顿的帖子:


至于
快速打印
,只需使用:

print >>sys.stderr, 'log msg'

--当然,它会登录到error.log中。

如果要将打印语句写入Apache错误日志,可以使用
sys.stderr

import sys
sys.stderr.write('log mgs')

然后它将出现在apache错误日志文件中。

特别要注意的是,如果使用mod_wsgi 3.X或更高版本,那么这根本不是问题,因为默认值已更改为允许使用sys.stdout,因为太多人懒得修复代码。只希望您现在不需要使用任何CGI/WSGI适配器,因为如果使用它们打印到sys.stdout,事情会搞砸。另一种快速打印到stderr的方法是
sys.stderr.write('log mgs')