-í
æ¶<c       s¶   d  Z  d k Z d k Z d k Z d k Z d e i f d „  ƒ  YZ d „  Z d e i f d „  ƒ  YZ	 e
 d j o@ e	 d d	 f ƒ Z e i e ƒ e i d
 „  d ƒ e i ƒ  n d S(   s½  Simple XML-RPC Server.

This module can be used to create simple XML-RPC servers
by creating a server and either installing functions, a
class instance, or by extending the SimpleXMLRPCRequestHandler
class.

A list of possible usage patterns follows:

1. Install functions:

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

2. Install an instance:

class MyFuncs:
    def __init__(self):
        # make all of the string functions available through
        # string.func_name
        import string
        self.string = string
    def pow(self, x, y): return pow(x, y)
    def add(self, x, y) : return x + y
server = SimpleXMLRPCServer(("localhost", 8000))
server.register_instance(MyFuncs())
server.serve_forever()

3. Install an instance with custom dispatch method:

class Math:
    def _dispatch(self, method, params):
        if method == 'pow':
            return apply(pow, params)
        elif method == 'add':
            return params[0] + params[1]
        else:
            raise 'bad method'
server = SimpleXMLRPCServer(("localhost", 8000))
server.register_instance(Math())
server.serve_forever()

4. Subclass SimpleXMLRPCRequestHandler:

class MathHandler(SimpleXMLRPCRequestHandler):
    def _dispatch(self, method, params):
        try:
            # We are forcing the 'export_' prefix on methods that are
            # callable through XML-RPC to prevent potential security
            # problems
            func = getattr(self, 'export_' + method)
        except AttributeError:
            raise Exception('method "%s" is not supported' % method)
        else:
            return apply(func, params)

    def log_message(self, format, *args):
        pass # maybe do something fancy like write the messages to a file

    def export_add(self, x, y):
        return x + y

server = SimpleXMLRPCServer(("localhost", 8000), MathHandler)
server.serve_forever()
Ns   SimpleXMLRPCRequestHandlerc      s/   t  Z d  Z d „  Z d „  Z d d d „ Z RS(   sg  Simple XML-RPC request handler class.

    Handles all HTTP POST requests and attempts to decode them as
    XML-RPC requests.

    XML-RPC requests are dispatched to the _dispatch method, which
    may be overriden by subclasses. The default implementation attempts
    to dispatch XML-RPC calls to the functions or instance installed
    in the server.
    c    s6  y¡ |  i i t |  i d ƒ ƒ } t i | ƒ \ } } y |  i
 | | ƒ } | f } Wn2 t i t i d d t i t i f ƒ ƒ } n Xt i | d d ƒ} Wn |  i d ƒ |  i ƒ  nq X|  i d ƒ |  i d d ƒ |  i d	 t t | ƒ ƒ ƒ |  i ƒ  |  i i | ƒ |  i i ƒ  |  i i d ƒ d
 S(   s±   Handles the HTTP POST request.

        Attempts to interpret all HTTP POST requests as XML-RPC calls,
        which are forwarded to the _dispatch method for handling.
        s   content-lengthi   s   %s:%ss   methodresponseiô  iÈ   s   Content-types   text/xmls   Content-lengthN(   s   selfs   rfiles   reads   ints   headerss   datas	   xmlrpclibs   loadss   paramss   methods	   _dispatchs   responses   dumpss   Faults   syss   exc_types	   exc_values   send_responses   end_headerss   send_headers   strs   lens   wfiles   writes   flushs
   connections   shutdown(   s   selfs   paramss   datas   methods   response(    (    s(   /usr/lib/python2.2/SimpleXMLRPCServer.pys   do_POSTY s(     /
c    sØ   t  } y |  i i | } Wn‰ t j
 o} |  i i t  j	 oc t |  i i d ƒ o |  i i i	 | | ƒ Sn0 y t |  i i | ƒ } Wn t j
 o n Xn n X| t  j	 o t | | ƒ Sn t d | ƒ ‚ d S(   s
  Dispatches the XML-RPC method.

        XML-RPC calls are forwarded to a registered function that
        matches the called XML-RPC method name. If no such function
        exists then the call is forwarded to the registered instance,
        if available.

        If the registered instance has a _dispatch method then that
        method will be called with the name of the XML-RPC method and
        it's parameters as a tuple
        e.g. instance._dispatch('add',(2,3))

        If the registered instance does not have a _dispatch method
        then the instance will be searched to find a matching method
        and, if found, will be called.

        Methods beginning with an '_' are considered private and will
        not be called by SimpleXMLRPCServer.
        s	   _dispatchs   method "%s" is not supportedN(   s   Nones   funcs   selfs   servers   funcss   methods   KeyErrors   instances   hasattrs	   _dispatchs   paramss   _resolve_dotted_attributes   AttributeErrors   applys	   Exception(   s   selfs   methods   paramss   func(    (    s(   /usr/lib/python2.2/SimpleXMLRPCServer.pys	   _dispatch s      s   -c    s+   |  i i o t i i |  | | ƒ n d S(   s$   Selectively log an accepted request.N(   s   selfs   servers   logRequestss   BaseHTTPServers   BaseHTTPRequestHandlers   log_requests   codes   size(   s   selfs   codes   size(    (    s(   /usr/lib/python2.2/SimpleXMLRPCServer.pys   log_request® s     (   s   __name__s
   __module__s   __doc__s   do_POSTs	   _dispatchs   log_request(    (    (    s(   /usr/lib/python2.2/SimpleXMLRPCServer.pys   SimpleXMLRPCRequestHandlerM s   
 	(	-c    sU   xJ | i d ƒ D]9 } | i d ƒ o t d | ƒ ‚ n t |  | ƒ }  q W|  Sd S(   s‚   Resolves a dotted attribute name to an object.  Raises
    an AttributeError if any attribute in the chain starts with a '_'.
    s   .s   _s(   attempt to access private attribute "%s"N(   s   attrs   splits   is
   startswiths   AttributeErrors   getattrs   obj(   s   objs   attrs   i(    (    s(   /usr/lib/python2.2/SimpleXMLRPCServer.pys   _resolve_dotted_attributeµ s      s   SimpleXMLRPCServerc      s2   t  Z d  Z e d d „ Z d „  Z e d „ Z RS(   sŠ   Simple XML-RPC server.

    Simple XML-RPC server that allows functions and a single instance
    to be installed to handle requests.
    i   c    s5   h  |  _ | |  _ t |  _ t i i |  | | ƒ d  S(   N(
   s   selfs   funcss   logRequestss   Nones   instances   SocketServers	   TCPServers   __init__s   addrs   requestHandler(   s   selfs   addrs   requestHandlers   logRequests(    (    s(   /usr/lib/python2.2/SimpleXMLRPCServer.pys   __init__Ê s    			c    s   | |  _  d S(   sõ  Registers an instance to respond to XML-RPC requests.

        Only one instance can be installed at a time.

        If the registered instance has a _dispatch method then that
        method will be called with the name of the XML-RPC method and
        it's parameters as a tuple
        e.g. instance._dispatch('add',(2,3))

        If the registered instance does not have a _dispatch method
        then the instance will be searched to find a matching method
        and, if found, will be called.

        Methods beginning with an '_' are considered private and will
        not be called by SimpleXMLRPCServer.

        If a registered function matches a XML-RPC request, then it
        will be called instead of the registered instance.
        N(   s   instances   self(   s   selfs   instance(    (    s(   /usr/lib/python2.2/SimpleXMLRPCServer.pys   register_instanceÑ s     c    s+   | t j o | i } n | |  i | <d S(   s  Registers a function to respond to XML-RPC requests.

        The optional name argument can be used to set a Unicode name
        for the function.

        If an instance is also registered then it will only be called
        if a matching function is not found.
        N(   s   names   Nones   functions   __name__s   selfs   funcs(   s   selfs   functions   name(    (    s(   /usr/lib/python2.2/SimpleXMLRPCServer.pys   register_functionè s     (   s   __name__s
   __module__s   __doc__s   SimpleXMLRPCRequestHandlers   __init__s   register_instances   Nones   register_function(    (    (    s(   /usr/lib/python2.2/SimpleXMLRPCServer.pys   SimpleXMLRPCServerÃ s    	s   __main__s	   localhosti@  c    s   |  | S(   N(   s   xs   y(   s   xs   y(    (    s(   /usr/lib/python2.2/SimpleXMLRPCServer.pys   <lambda>ù s    s   add(   s   __doc__s	   xmlrpclibs   SocketServers   BaseHTTPServers   syss   BaseHTTPRequestHandlers   SimpleXMLRPCRequestHandlers   _resolve_dotted_attributes	   TCPServers   SimpleXMLRPCServers   __name__s   servers   register_functions   pows   serve_forever(   s   syss   _resolve_dotted_attributes   SocketServers   SimpleXMLRPCRequestHandlers   BaseHTTPServers	   xmlrpclibs   SimpleXMLRPCServers   server(    (    s(   /usr/lib/python2.2/SimpleXMLRPCServer.pys   ?C s   				h	3