;ò
ëŒü?c           @   s  d  Z  d k Z d k l Z d k Z d k Z d k Z d k Z d k Z 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   sU   xJ | i d ƒ D]9 } | i d ƒ o t d | ƒ ‚ q t |  | ƒ }  q W|  Sd S(   s·   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 '_'.
    s   .s   _s(   attempt to access private attribute "%s"N(   s   attrs   splits   is
   startswiths   AttributeErrors   getattrs   obj(   s   objs   attrs   i(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   resolve_dotted_attributen   s      c         C   sZ   g  i  } t |  ƒ D]; } | i d ƒ o t t |  | ƒ ƒ o | | ƒ q q ~ Sd S(   sk   Returns a list of attribute strings, found in the specified
    object, which represent callable attributess   _N(   s   appends   _[1]s   dirs   objs   members
   startswiths   callables   getattr(   s   objs   members   _[1](    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   list_public_methods~   s     c         C   s/   h  } x |  D] } d | | <q W| i ƒ  Sd 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(   s   us   lsts   xs   keys(   s   lsts   xs   u(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   remove_duplicates†   s      s   SimpleXMLRPCDispatcherc           B   sw   t  Z d  Z d „  Z 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  |  _ t |  _ d  S(   N(   s   selfs   funcss   Nones   instance(   s   self(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   __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
        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/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   register_instanceŸ   s     c         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.
        N(   s   names   Nones   functions   __name__s   selfs   funcs(   s   selfs   functions   name(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   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(   s   selfs   funcss   updates   system_listMethodss   system_methodSignatures   system_methodHelp(   s   self(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys    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(   s   selfs   funcss   updates   system_multicall(   s   self(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   register_multicall_functionsË   s     c         C   sÉ   t  i | ƒ \ } } yT | t 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| Sd 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.
        s   methodresponsei   s   %s:%sN(   s	   xmlrpclibs   loadss   datas   paramss   methods   dispatch_methods   Nones   responses   selfs	   _dispatchs   dumpss   Faults   faults   syss   exc_types	   exc_value(   s   selfs   datas   dispatch_methods   faults   responses   paramss   method(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   _marshaled_dispatchÓ   s    
 	/c         C   s–   |  i i ƒ  } |  i t j	 oe t |  i d ƒ o t | |  i i ƒ  ƒ } q„ t |  i d ƒ o t | t	 |  i ƒ ƒ } q„ n | i
 ƒ  | Sd S(   sw   system.listMethods() => ['add', 'subtract', 'multiple']

        Returns a list of the methods supported by the server.s   _listMethodss	   _dispatchN(   s   selfs   funcss   keyss   methodss   instances   Nones   hasattrs   remove_duplicatess   _listMethodss   list_public_methodss   sort(   s   selfs   methods(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   system_listMethodsô   s     !
c         C   s   d Sd S(   s#  system.methodSignature('add') => [double, int, int]

        Returns a list describing the signiture 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(    (   s   selfs   method_name(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   system_methodSignature  s     c         C   sØ   t  } |  i i | ƒ o |  i | } n€ |  i t  j	 oo t |  i d ƒ o |  i i | ƒ Sq© t |  i d ƒ o0 y t	 |  i | ƒ } Wq¥ t
 j
 o q¥ Xq© n | t  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.s   _methodHelps	   _dispatchs    N(   s   Nones   methods   selfs   funcss   has_keys   method_names   instances   hasattrs   _methodHelps   resolve_dotted_attributes   AttributeErrors   pydocs   getdoc(   s   selfs   method_names   pydocs   method(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   system_methodHelp  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| Sd 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
        s
   methodNames   paramss	   faultCodes   faultStringi   s   %s:%sN(   s   resultss	   call_lists   calls   method_names   paramss   appends   selfs	   _dispatchs   Faults   faults	   faultCodes   faultStrings   syss   exc_types	   exc_value(   s   selfs	   call_lists   faults   resultss   method_names   paramss   call(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   system_multicall7  s      

 )7c         C   sÆ   t  } y |  i | } Wn} t j
 oq |  i t  j	 oZ t |  i d ƒ o |  i i | | ƒ Sq’ y t
 |  i | ƒ } Wq’ t j
 o q’ Xq— n X| t  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
        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.
        s	   _dispatchs   method "%s" is not supportedN(   s   Nones   funcs   selfs   funcss   methods   KeyErrors   instances   hasattrs	   _dispatchs   paramss   resolve_dotted_attributes   AttributeErrors	   Exception(   s   selfs   methods   paramss   func(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys	   _dispatchV  s      	(   s   __name__s
   __module__s   __doc__s   __init__s   register_instances   Nones   register_functions    register_introspection_functionss   register_multicall_functionss   _marshaled_dispatchs   system_listMethodss   system_methodSignatures   system_methodHelps   system_multicalls	   _dispatch(    (    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   SimpleXMLRPCDispatcher“   s    				!				s   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 t	 ƒ ƒ } 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-lengths	   _dispatchiô  iÈ   s   Content-types   text/xmls   Content-lengthi   N(   s   selfs   rfiles   reads   ints   headerss   datas   servers   _marshaled_dispatchs   getattrs   Nones   responses   send_responses   end_headerss   send_headers   strs   lens   wfiles   writes   flushs
   connections   shutdown(   s   selfs   datas   response(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   do_POSTŠ  s     %
s   -c         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/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   log_request¬  s     (   s   __name__s
   __module__s   __doc__s   do_POSTs   log_request(    (    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   SimpleXMLRPCRequestHandlerƒ  s    	"s   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(   s   logRequestss   selfs   SimpleXMLRPCDispatchers   __init__s   SocketServers	   TCPServers   addrs   requestHandler(   s   selfs   addrs   requestHandlers   logRequests(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   __init__½  s    	(   s   __name__s
   __module__s   __doc__s   SimpleXMLRPCRequestHandlers   __init__(    (    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   SimpleXMLRPCServer²  s   	 s   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(   s   SimpleXMLRPCDispatchers   __init__s   self(   s   self(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   __init__Ç  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(   s   selfs   _marshaled_dispatchs   request_texts   responses   lens   syss   stdouts   write(   s   selfs   request_texts   response(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   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  s   codes   messages   explains   Status: %d %ss   Content-Type: text/htmls   Content-Length: %dN(   s   codes   BaseHTTPServers   BaseHTTPRequestHandlers	   responsess   messages   explains   DEFAULT_ERROR_MESSAGEs   responses   lens   syss   stdouts   write(   s   selfs   codes   explains   responses   message(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys
   handle_getÔ  s     (c         C   sh   | t j o t i i d t ƒ d j o |  i ƒ  n. | t 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.
        s   REQUEST_METHODs   GETN(   s   request_texts   Nones   oss   environs   gets   selfs
   handle_gets   syss   stdins   reads   handle_xmlrpc(   s   selfs   request_text(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   handle_requestë  s     )(   s   __name__s
   __module__s   __doc__s   __init__s   handle_xmlrpcs
   handle_gets   Nones   handle_request(    (    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   CGIXMLRPCRequestHandlerÄ  s
    		
	s   __main__s	   localhosti@  c         C   s   |  | S(   N(   s   xs   y(   s   xs   y(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   <lambda>   s    s   add(   s   __doc__s	   xmlrpclibs   Faults   SocketServers   BaseHTTPServers   syss   typess   oss   resolve_dotted_attributes   list_public_methodss   remove_duplicatess   SimpleXMLRPCDispatchers   BaseHTTPRequestHandlers   SimpleXMLRPCRequestHandlers	   TCPServers   SimpleXMLRPCServers   CGIXMLRPCRequestHandlers   __name__s   servers   register_functions   pows   serve_forever(   s   remove_duplicatess   SocketServers   SimpleXMLRPCRequestHandlers   Faults	   xmlrpclibs   SimpleXMLRPCServers   CGIXMLRPCRequestHandlers   syss   list_public_methodss   SimpleXMLRPCDispatchers   BaseHTTPServers   resolve_dotted_attributes   servers   oss   types(    (    s/   /mit/python/lib/python2.3/SimpleXMLRPCServer.pys   ?a   s&   									ð/9