[Twisted-Python] Resource perplexity

Steve Waterbury waterbug at beeblebrox.gsfc.nasa.gov
Mon Aug 12 17:10:29 EDT 2002


Yet another newbie question:
Firewall realities being what they are, I'd like to "multiplex" several 
things over a single web server/port (80 for production, but for now 
anything), so I've been hacking at the xmlrpc example, but no luck so 
far (see attached attempt -- it runs, but gives a "404" for 
/RPC2 or /RPC2/ ...).  

Two questions:
(1) Is this an architecturally sound use of twisted?  (Optimal?)
    (And if not, suggestions?  :^)
(2) If so, what am I doing blockheaded?  

Thanks again for your help and patience!
-- Steve.

Stephen C. Waterbury  http://misspiggy.gsfc.nasa.gov/people/waterbug.html
-------------- next part --------------
"""
Derived from twisted xmlrpc example ... 
Adds a resource_mux resource to dispatch requests based on URL.
-----------------------------------------------------------------
Hook up an object to XML-RPC. An example session:
    
    >>> s = xmlrpclib.Server('http://localhost:7080/RPC2/')
    >>> s.echo("lala")
    ['lala']
    >>> s.echo("lala", 1)
    ['lala', 1]
    >>> s.echo("lala", 4)
    ['lala', 4]
    >>> s.echo("lala", 4, 3.4)
    ['lala', 4, 3.3999999999999999]
    >>> s.echo("lala", 4, [1, 2])
    ['lala', 4, [1, 2]]
"""

from twisted.web import xmlrpc, resource
from twisted.internet import defer
import xmlrpclib


class Echoer(xmlrpc.XMLRPC):
    """An example object to be published.
    
    Has five methods accessable by XML-RPC, 'echo', 'hello', 'defer's.echo('spam'),
    'defer_fail' and 'fail.
    """
    
    def xmlrpc_echo(self, *args):
        """Return all passed args."""
        return args
    
    def xmlrpc_hello(self):
        """Return 'hello, world'."""
        return 'hello, world!'
    
    def xmlrpc_defer(self):
        """Show how xmlrpc methods can return Deferred."""
        return defer.succeed("hello")
    
    def xmlrpc_defer_fail(self):
        """Show how xmlrpc methods can return failed Deferred."""
        return defer.fail(12)

    def xmlrpc_fail(self):
        """Show how we can return a failure code."""
        return xmlrpclib.Fault(7, "Out of cheese.")


def main():
    from twisted.internet.app import Application
    from twisted.web import server
    # Resource multiplexer 'mux'; dispatches requests to Web,
    # XML-RPC, or SOAP handler, depending on base URL.  Standard
    # base URLs: 'WWW', 'RPC2', 'SOAP' (probably should be
    # case-insensitive).
    mux = resource.Resource()
    mux.putChild('/RPC2/', Echoer())
    # similarly for other handlers ...
    app = Application("thingy")
    app.listenTCP(7080, server.Site(mux))
    app.run(0)


if __name__ == '__main__':
    main()


More information about the Twisted-Python mailing list