How to handle Python XML-RPC output and exceptions?

I created a simple Python implementation of XML-RPC , mainly based on examples.

However, it sends the output as follows:

foo.bar.com - - [13/Feb/2010 17:55:47] "POST /RPC2 HTTP/1.0" 200 - 

... to the terminal, even if I redirect the standard and standard error to a file using >> or > . I am doing this with the following line:

 python foobar 2>&1 >> foobar.log 

It seems that it is not being sent to the standard version, but somewhere else.

In addition, when an exception occurs when a request is received, the entire application error with this error:

 ---------------------------------------- Exception happened during processing of request from ('1.2.3.4', 51284) 

How can I handle this exception? I need to restore the grace and just write an exception message, not a server crash.

+4
source share
2 answers

I think you are using the SimpleXMLRPCServer class from the examples. In this case, just create the logRequests parameter when you create it:

 server = SimpleXMLRPCServer(("localhost", 8000), logRequests = False) 

This will suppress the registration of requests.

As for the exceptions, they are logged in to the BaseServer system (see the source code "SocketServer.py"):

 def handle_error(self, request, client_address): """Handle an error gracefully. May be overridden. The default is to print a traceback and continue. """ print '-'*40 print 'Exception happened during processing of request from', print client_address import traceback traceback.print_exc() # XXX But this goes to stderr! print '-'*40 

As you can see, the first part is written to stdout, therefore &2>1 does not work completely. If you want to suppress them, override or overwrite this method.

+5
source

This is really a shell redirection issue, not a python issue. When you say

 python foobar 2>&1 >> foobar.log 

First you redirect stderr to stdout, and then the second redirect stdout to foobar.log. stderr is not automatically redirected to foobar.log. stderr still points to the original output.

So do the following:

 python foobar >> foobar.log 2>&1 

See also http://bash-hackers.org/wiki/doku.php/syntax/redirection (Search for "Multiple Redirects")

0
source

Source: https://habr.com/ru/post/1301203/


All Articles