m
fIc           @   s  d  Z  d k Z d k l Z d k Z d k Z d k Z d k Z e d  Z d   Z	 d   Z
 d f  d     YZ d e i f d	     YZ d
 e i e f d     YZ d e f d     YZ e d j o@ e d d f  Z e i e  e i d   d  e i   n d S(   s9  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 SimpleXMLRPCServer
class.

It can also be used to handle XML-RPC requests in a CGI
environment using CGIXMLRPCRequestHandler.

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 _listMethods(self):
        # implement this method so that system.listMethods
        # knows to advertise the strings methods
        return list_public_methods(self) +                 ['string.' + method for method in list_public_methods(self.string)]
    def pow(self, x, y): return pow(x, y)
    def add(self, x, y) : return x + y

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

3. Install an instance with custom dispatch method:

class Math:
    def _listMethods(self):
        # this method must be present for system.listMethods
        # to work
        return ['add', 'pow']
    def _methodHelp(self, method):
        # this method must be present for system.methodHelp
        # to work
        if method == 'add':
            return "add(2,3) => 5"
        elif method == 'pow':
            return "pow(x, y[, z]) => number"
        else:
            # By convention, return empty
            # string if no help is available
            return ""
    def _dispatch(self, method, params):
        if method == 'pow':
            return pow(*params)
        elif method == 'add':
            return params[0] + params[1]
        else:
            raise 'bad method'

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

4. Subclass SimpleXMLRPCServer:

class MathServer(SimpleXMLRPCServer):
    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 func(*params)

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

server = MathServer(("localhost", 8000))
server.serve_forever()

5. CGI script:

server = CGIXMLRPCRequestHandler()
server.register_function(pow)
server.handle_request()
N(   s   Faultc         C   sk   | o | i d  } n
 | g } xA | D]9 } | i d  o t d |   q* t |  |  }  q* W|  S(   sG  resolve_dotted_attribute(a, 'b.c.d') => a.b.c.d

    Resolves a dotted attribute name to an object.  Raises
    an AttributeError if any attribute in the chain starts with a '_'.

    If the optional allow_dotted_names argument is false, dots are not
    supported and this function operates similar to getattr(obj, attr).
    t   .t   _s(   attempt to access private attribute "%s"N(	   t   allow_dotted_namest   attrt   splitt   attrst   it
   startswitht   AttributeErrort   getattrt   obj(   R
   R   R   R   R   (    (    t/   /mit/python/lib/python2.4/SimpleXMLRPCServer.pyt   resolve_dotted_attributem   s     	 c         C   sP   g  } t |   D]8 } | i d  o! t t |  |   o | | q q ~ S(   sk   Returns a list of attribute strings, found in the specified
    object, which represent callable attributesR   N(   t   _[1]t   dirR
   t   memberR   t   callableR	   (   R
   R   R   (    (    R   t   list_public_methods   s     c         C   s+   h  } x |  D] } d | | <q W| i   S(   s   remove_duplicates([2,2,2,1,3,3]) => [3,1,2]

    Returns a copy of a list without duplicates. Every list
    item must be hashable and the order of the items in the
    resulting list is not defined.
    i   N(   t   ut   lstt   xt   keys(   R   R   R   (    (    R   t   remove_duplicates   s      t   SimpleXMLRPCDispatcherc           B   sz   t  Z d  Z d   Z e d  Z e d  Z d   Z d   Z	 e d  Z
 d   Z d   Z d	   Z d
   Z d   Z RS(   s   Mix-in class that dispatches XML-RPC requests.

    This class is used to register XML-RPC method handlers
    and then to dispatch them. There should never be any
    reason to instantiate this class directly.
    c         C   s   h  |  _ d  |  _ d  S(   N(   t   selft   funcst   Nonet   instance(   R   (    (    R   t   __init__   s    	c         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
        its 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.

        If the optional allow_dotted_names argument is true and the
        instance does not have a _dispatch method, method names
        containing dots are supported and resolved, as long as none of
        the name segments start with an '_'.

            *** SECURITY WARNING: ***

            Enabling the allow_dotted_names options allows intruders
            to access your module's global variables and may allow
            intruders to execute arbitrary code on your machine.  Only
            use this option on a secure, closed network.

        N(   R   R   R   (   R   R   R   (    (    R   t   register_instance   s     	c         C   s+   | d 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.
        N(   t   nameR   t   functiont   __name__R   R   (   R   R   R   (    (    R   t   register_function   s     c         C   s8   |  i i h  d |  i <d |  i <d |  i < d S(   s   Registers the XML-RPC introspection methods in the system
        namespace.

        see http://xmlrpc.usefulinc.com/doc/reserved.html
        s   system.listMethodss   system.methodSignatures   system.methodHelpN(   R   R   t   updatet   system_listMethodst   system_methodSignaturet   system_methodHelp(   R   (    (    R   t    register_introspection_functions   s     c         C   s    |  i i h  d |  i < d S(   s   Registers the XML-RPC multicall method in the system
        namespace.

        see http://www.xmlrpc.com/discuss/msgReader$1208s   system.multicallN(   R   R   R"   t   system_multicall(   R   (    (    R   t   register_multicall_functions   s     c         C   s   t  i |  \ } } yT | d j	 o | | |  } n |  i	 | |  } | f } t  i
 | d d } WnU t j
 o } t  i
 |  } n3 t  i
 t  i d d t i t i f   } n X| S(   s  Dispatches an XML-RPC method from marshalled (XML) data.

        XML-RPC methods are dispatched from the marshalled (XML) data
        using the _dispatch method and the result is returned as
        marshalled data. For backwards compatibility, a dispatch
        function can be provided as an argument (see comment in
        SimpleXMLRPCRequestHandler.do_POST) but overriding the
        existing method through subclassing is the prefered means
        of changing method dispatch behavior.
        t   methodresponsei   s   %s:%sN(   t	   xmlrpclibt   loadst   datat   paramst   methodt   dispatch_methodR   t   responseR   t	   _dispatcht   dumpst   Faultt   faultt   syst   exc_typet	   exc_value(   R   R,   R/   R4   R0   R-   R.   (    (    R   t   _marshaled_dispatch   s    
 	/c         C   s   |  i i   } |  i d j	 od t |  i d  o t | |  i i    } q t |  i d  p t | t	 |  i   } q n | i
   | S(   sw   system.listMethods() => ['add', 'subtract', 'multiple']

        Returns a list of the methods supported by the server.t   _listMethodsR1   N(   R   R   R   t   methodsR   R   t   hasattrR   R9   R   t   sort(   R   R:   (    (    R   R#   	  s     !
c         C   s   d S(   s#  system.methodSignature('add') => [double, int, int]

        Returns a list describing the signature of the method. In the
        above example, the add method takes two integers as arguments
        and returns a double result.

        This server does NOT support system.methodSignature.s   signatures not supportedN(    (   R   t   method_name(    (    R   R$      s     c         C   s   d } |  i i |  o |  i | } n |  i d j	 ot t |  i d  o |  i i |  Sq t |  i d  p6 y t	 |  i | |  i
  } Wq t j
 o q Xq n | d j o d Sn d k } | i |  Sd S(   s   system.methodHelp('add') => "Adds two integers together"

        Returns a string containing documentation for the specified method.t   _methodHelpR1   t    N(   R   R.   R   R   t   has_keyR=   R   R;   R>   R   R   R   t   pydoct   getdoc(   R   R=   RA   R.   (    (    R   R%   -  s$     		c         C   s   g  } x | D] } | d } | d } y  | i |  i | |  g  Wq t j
 o. } | i h  d | i
 <d | i < q | i h  d d <d d t i t i f < q Xq W| S(   s   system.multicall([{'methodName': 'add', 'params': [2, 2]}, ...]) => [[4], ...]

        Allows the caller to package multiple XML-RPC calls into a single
        request.

        See http://www.xmlrpc.com/discuss/msgReader$1208
        t
   methodNameR-   t	   faultCodet   faultStringi   s   %s:%sN(   t   resultst	   call_listt   callR=   R-   t   appendR   R1   R3   R4   RD   RE   R5   R6   R7   (   R   RG   R4   RF   R=   R-   RH   (    (    R   R'   M  s      

 )7c         C   s   d } y |  i | } Wn t j
 ow |  i d j	 o` t |  i d  o |  i i | |  Sq y t
 |  i | |  i  } Wq t j
 o q Xq n X| d j	 o | |   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
        its 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.
        R1   s   method "%s" is not supportedN(   R   t   funcR   R   R.   t   KeyErrorR   R;   R1   R-   R   R   R   t	   Exception(   R   R.   R-   RJ   (    (    R   R1   l  s"     	(   R    t
   __module__t   __doc__R   t   FalseR   R   R!   R&   R(   R8   R#   R$   R%   R'   R1   (    (    (    R   R      s    	$		!			 	t   SimpleXMLRPCRequestHandlerc           B   s&   t  Z d  Z d   Z d d d  Z RS(   s   Simple XML-RPC request handler class.

    Handles all HTTP POST requests and attempts to decode them as
    XML-RPC requests.
    c         C   s   yD |  i i t |  i d   } |  i i | t |  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 server's _dispatch method for handling.
        s   content-lengthR1   i  i   s   Content-types   text/xmls   Content-lengthi   N(   R   t   rfilet   readt   intt   headersR,   t   serverR8   R	   R   R0   t   send_responset   end_headerst   send_headert   strt   lent   wfilet   writet   flusht
   connectiont   shutdown(   R   R,   R0   (    (    R   t   do_POST  s     %
t   -c         C   s+   |  i i o t i i |  | |  n d S(   s$   Selectively log an accepted request.N(   R   RU   t   logRequestst   BaseHTTPServert   BaseHTTPRequestHandlert   log_requestt   codet   size(   R   Rf   Rg   (    (    R   Re     s     (   R    RM   RN   R`   Re   (    (    (    R   RP     s    	"t   SimpleXMLRPCServerc           B   s   t  Z d  Z e d d  Z RS(   sg  Simple XML-RPC server.

    Simple XML-RPC server that allows functions and a single instance
    to be installed to handle requests. The default implementation
    attempts to dispatch XML-RPC calls to the functions or instance
    installed in the server. Override the _dispatch method inhereted
    from SimpleXMLRPCDispatcher to change this behavior.
    i   c         C   s0   | |  _  t i |   t i i |  | |  d  S(   N(   Rb   R   R   R   t   SocketServert	   TCPServert   addrt   requestHandler(   R   Rk   Rl   Rb   (    (    R   R     s    	(   R    RM   RN   RP   R   (    (    (    R   Rh     s   	 t   CGIXMLRPCRequestHandlerc           B   s5   t  Z d  Z d   Z d   Z d   Z e d  Z RS(   s3   Simple handler for XML-RPC data passed through CGI.c         C   s   t  i |   d  S(   N(   R   R   R   (   R   (    (    R   R     s    c         C   s8   |  i |  } d GHd t |  GHHt i i |  d S(   s   Handle a single XML-RPC requests   Content-Type: text/xmls   Content-Length: %dN(   R   R8   t   request_textR0   RZ   R5   t   stdoutR\   (   R   Rn   R0   (    (    R   t   handle_xmlrpc  s     c         C   s|   d } t i i | \ } } t i h  d | <d | <d | <} d | | f GHd GHd t |  GHHt	 i
 i |  d S(	   s   Handle a single HTTP GET request.

        Default implementation indicates an error because
        XML-RPC uses the POST method.
        i  Rf   t   messaget   explains   Status: %d %ss   Content-Type: text/htmls   Content-Length: %dN(   Rf   Rc   Rd   t	   responsesRq   Rr   t   DEFAULT_ERROR_MESSAGER0   RZ   R5   Ro   R\   (   R   Rf   Rr   R0   Rq   (    (    R   t
   handle_get  s     (c         C   sh   | d j o* t i i d d  d j o |  i   n. | d j o t i i	   } n |  i
 |  d S(   s   Handle a single XML-RPC request passed through a CGI post method.

        If no XML data is given then it is read from stdin. The resulting
        XML-RPC response is printed to stdout along with the correct HTTP
        headers.
        t   REQUEST_METHODt   GETN(   Rn   R   t   ost   environt   getR   Ru   R5   t   stdinRR   Rp   (   R   Rn   (    (    R   t   handle_request  s     )(   R    RM   RN   R   Rp   Ru   R   R|   (    (    (    R   Rm     s
    		
	t   __main__t	   localhosti@  c         C   s   |  | S(   N(   R   t   y(   R   R   (    (    R   t   <lambda>  s    t   add(   RN   R*   R3   Ri   Rc   R5   Rx   t   TrueR   R   R   R   Rd   RP   Rj   Rh   Rm   R    RU   R!   t   powt   serve_forever(   R   Ri   RP   R3   R*   Rh   Rm   R5   R   R   Rc   R   RU   Rx   (    (    R   t   ?a   s&   							 /9