tags:

views:

185

answers:

2

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.

+3  A: 

I guess you're using the SimpleXMLRPCServer class from the examples. In that case, simply provide the parameter logRequests when creating it:

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

That will suppress request logging.

As for the exceptions, they're logged in BaseServer (cf. source code of "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, which is why &2>1 didn't work completely. If you want to suppress them, override or overwrite that method.

AndiDog
How would I redirect log requests to somewhere else? For example, an instance of `logging.getLogger()`. Also, how is the application crashing when the exception is printed?
nbolton
As you can see in the quoted code ("SocketServer.py" from the standard library, which is used indirectly by `xmlrpclib`), the app does not crash when an exception happened, it just gets ignored. If you want to redirect such logs, you could write your own `BaseServer`/`BaseHTTPServer`/`SimpleXMLRPCServer` classes (deriving from each other), or simply overwrite the logging functions, e.g. `SocketServer.BaseServer.handle_error = yourLogHandlerWhichWritesToLogger`.
AndiDog
A: 

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

python foobar 2>&1 >> foobar.log

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:

python foobar >> foobar.log 2>&1 

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

unutbu
I could never get the hang of output redirection ha. Maybe I never will... I use this a lot in my crontab, so maybe I should check that first in future for reference.
nbolton