How to Catch Errors in SimpleXMLRPCServer

11 views
Skip to first unread message

gregp...@gmail.com

unread,
Sep 27, 2007, 3:35:45 PM9/27/07
to
I have a pretty simple XMLRPCServer, something along the lines of the
example:

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_function(pow)
server.register_function(lambda x,y: x+y, 'add')
server.serve_forever()

Now what I want to do is catch any errors that happen on requests, and
ideally have them emailed to me. I have the email part all taken care
of, I just need to know how to get at the exceptions.

Thanks in advance for any help,

Greg

Bruno Desthuilliers

unread,
Sep 27, 2007, 3:48:22 PM9/27/07
to
gregp...@gmail.com a écrit :

Q&D :

try:
server.serve_forever()
except Exception, e:
mail_me_the_exception(e)
raise

Jeff McNeil

unread,
Sep 27, 2007, 3:55:52 PM9/27/07
to gregp...@gmail.com, pytho...@python.org
Instead of register_function, use register_instance and provide a
_dispatch method in that instance that handles your exception logging.

Pseudo:

class MyCalls(object):
def _dispatch(self, method, args):
try:
self.getattr(self, method)(*args)
except:
handle_logging()

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

server.register_instance(MyCalls())
server.serve_forever()

There might be an easier way... but this works for me.

-Jeff

> --
> http://mail.python.org/mailman/listinfo/python-list
>

Jeff McNeil

unread,
Sep 27, 2007, 3:56:27 PM9/27/07
to gregp...@gmail.com, pytho...@python.org
getattr, not self.getattr.

On 9/27/07, Jeff McNeil <je...@jmcneil.net> wrote:
> Instead of register_function, use register_instance and provide a
> _dispatch method in that instance that handles your exception logging.
>
> Pseudo:
>
> class MyCalls(object):
> def _dispatch(self, method, args):
> try:
> self.getattr(self, method)(*args)
> except:
> handle_logging()
>

> server = SimpleXMLRPCServer(("localhost", 8000))

> server.register_instance(MyCalls())
> server.serve_forever()
>
> There might be an easier way... but this works for me.
>
> -Jeff
>
> On 9/27/07, gregp...@gmail.com <gregp...@gmail.com> wrote:

> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>

gregp...@gmail.com

unread,
Sep 27, 2007, 4:55:33 PM9/27/07
to
On Sep 27, 3:55 pm, "Jeff McNeil" <j...@jmcneil.net> wrote:
> Instead of register_function, use register_instance and provide a
> _dispatch method in that instance that handles your exception logging.
>
> Pseudo:
>
> class MyCalls(object):
> def _dispatch(self, method, args):
> try:
> self.getattr(self, method)(*args)
> except:
> handle_logging()
>
> server = SimpleXMLRPCServer(("localhost", 8000))
> server.register_instance(MyCalls())
> server.serve_forever()
>
> There might be an easier way... but this works for me.

I wonder if there is something wrong with that. I get this error on
calling ever method:

Fault 1: 'exceptions.TypeError:cannot marshal None unless allow_none
is enabled' but I can't see anywhere None would be coming from.

-Greg


Jeff McNeil

unread,
Sep 27, 2007, 5:08:17 PM9/27/07
to gregp...@gmail.com, pytho...@python.org
Yeah, that code was out of memory and I didn't test it, my apologies.
Need to actually return a value from _dispatch.

class MyCalls(object):
def _dispatch(self, method, args):
try:

return getattr(self, method)(*args)
except:
handle_logging()

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_instance(MyCalls())
server.serve_forever()

-Jeff


On 9/27

> --
> http://mail.python.org/mailman/listinfo/python-list
>

gregp...@gmail.com

unread,
Sep 27, 2007, 8:01:57 PM9/27/07
to
On Sep 27, 5:08 pm, "Jeff McNeil" <j...@jmcneil.net> wrote:
> Yeah, that code was out of memory and I didn't test it, my apologies.
> Need to actually return a value from _dispatch.
>
> class MyCalls(object):
> def _dispatch(self, method, args):
> try:
> return getattr(self, method)(*args)
> except:
> handle_logging()
>
> server = SimpleXMLRPCServer(("localhost", 8000))
> server.register_instance(MyCalls())
> server.serve_forever()
>

Thanks, that works. I'm not sure why I didn't notice it wasn't
returning anything.

-Greg


Reply all
Reply to author
Forward
0 new messages