如何处理 Python XML-RPC 输出和异常?
我创建了一个简单的 Python XML-RPC 实现,主要基于示例。
它也会向终端发送如下输出:
foo.bar.com - - [13/Feb/2010 17:55:47] "POST /RPC2 HTTP/1.0" 200 -
但是,即使我使用 >>
或 >
将标准输出和标准错误重定向到文件, ... 。我使用以下行执行此操作:
python foobar 2>&1 >> foobar.log
看起来它几乎没有发送到标准输出,而是发送到其他地方。
此外,当接收请求时发生异常时,整个应用程序会崩溃并出现以下错误:
----------------------------------------
Exception happened during processing of request from ('1.2.3.4', 51284)
如何处理此异常?我需要优雅地恢复,只记录异常消息而不是服务器崩溃。
I have created a simple Python XML-RPC implementation, largely based on the examples.
However, it sends output like this:
foo.bar.com - - [13/Feb/2010 17:55:47] "POST /RPC2 HTTP/1.0" 200 -
... to the terminal, even if I redirect standard out and standard error to a file using >>
or >
. I'm doing this with the following line:
python foobar 2>&1 >> foobar.log
It seems almost like it's not sending to standard out, but somewhere else.
Also, when an exception occurs on recieving a request, the whole application crashes with this error:
----------------------------------------
Exception happened during processing of request from ('1.2.3.4', 51284)
How can I handle this exception? I need to recover gracefully, and just log the exception message rather than the server crashing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜您正在使用示例中的
SimpleXMLRPCServer
类。在这种情况下,只需在创建时提供参数logRequests
即可:这将抑制请求日志记录。
至于例外情况,它们会记录在
BaseServer
中(参见“SocketServer.py”的源代码):如您所见,第一部分写入 stdout,这就是为什么
&2>1
没有完全工作。如果您想抑制它们,请覆盖或覆盖该方法。I guess you're using the
SimpleXMLRPCServer
class from the examples. In that case, simply provide the parameterlogRequests
when creating it:That will suppress request logging.
As for the exceptions, they're logged in
BaseServer
(cf. source code of "SocketServer.py"):As you can see, the first part is written to stdout, which is why
&2>1
didn't work completely. If you want to suppress them, override or overwrite that method.这实际上是一个 shell 重定向问题,而不是 python 问题。
当您说
“您首先将 stderr 重定向到 stdout,然后将 stdout 重定向到 foobar.log”时。
stderr 不会自动重定向到 foobar.log。 stderr 仍然指向原始的 stdout。
因此,请这样做:
另请参阅 http://bash-hackers.org/wiki /doku.php/syntax/redirection(搜索“多个重定向”)
This is really a shell redirection problem, not a python problem.
When you say
You are first redirecting stderr to stdout, and then second redirecting stdout to foobar.log.
stderr does not get automatically redirected to foobar.log. stderr still points to the original stdout.
So instead do this:
See also http://bash-hackers.org/wiki/doku.php/syntax/redirection (search for "Multiple redirections")