[mod_python] feedback on my XML-RPC handler?

Michael mogmios at mlug.missouri.edu
Sat Mar 6 03:23:07 EST 2004


This is my first stab at an XML-RPC handler for mod_python. I haven't 
really used mod_python before this so I thought maybe somee of you could 
take a peek and give me advice on if I'm doing things horribly wrong or 
not. Obviously I borrowed this mostly from Python's own 
CGIXMLRPCRequestHandler but with the needed changes to work with 
mod_python and to provide some documentation to visitors. Thanks.

----
from mod_python import apache
from mod_python import util
import SimpleXMLRPCServer

class MPXMLRPCRequestHandler ( SimpleXMLRPCServer.SimpleXMLRPCDispatcher ):
    """Simple handler for XML-RPC data passed through mod_python."""
    def __init__ ( self ):
        SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__ ( self )

    def handle_xmlrpc ( self, req ):
        """Handle a single XML-RPC request or a request for 
documentation."""
        if req.method == 'POST':
                response = self._marshaled_dispatch ( req.read () )
                req.content_type = "text/xml"
                req.send_http_header ()
                req.write ( response )
        elif req.method == 'GET':
                path = 'http://%s:%s%s' % ( req.server.server_hostname, 
req.server.port, req.parsed_uri[apache.URI_PATH] )
                req.content_type = "text/html"
                req.send_http_header ()
                FS = util.FieldStorage ( req )
                docs = ''
                if FS.has_key ( 'topic' ):
                        docs += '<a href="%s">back to topic 
list</a><h1>%s</h1>' % ( path, FS['topic'] )
                        docs += self.system_methodHelp ( FS['topic'] )
                else:
                        methods = self.system_listMethods ()
                        if methods:
                                for method in methods:
                                        docs += '<li><a 
href="%s?topic=%s">%s</a></li>' % ( path, method, method )
                req.write ( 
'<html><head><title>Documentation</title></head><body>%s</body></html>' 
% ( docs ) )

def test ( i ):
        """These are my test docs."""
        return i, i * 2

xmlrpc = MPXMLRPCRequestHandler ()
xmlrpc.register_function ( test )
xmlrpc.register_introspection_functions ()

def handler ( req ):
        global xmlrpc
        xmlrpc.handle_xmlrpc ( req )
        return apache.OK
----


More information about the Mod_python mailing list