Table of Contents
AMF Remoting gateway in CherryPy with PyAMF
Introduction
CherryPy 3.0+ allows you to graft any WSGI application as a controller. PyAMF's WSGI gateway can thus be used to easily expose a set of methods via AMF remoting.
Example
The following example shows how a single method is exposed in this way:
import cherrypy from pyamf.remoting.gateway.wsgi import WSGIGateway # This is a function that we will expose def echo(data): return data services = { 'myservice.echo': echo, # Add other exposed functions here } # This is the root controller for the rest of the website class Root(object): def index(self): return "This is your main website" index.exposed = True config = { '/crossdomain.xml': { 'tools.staticfile.on': True, 'tools.staticfile.filename': '/path/to/crossdomain.xml' } } # This is where we hook in the WSGIGateway cherrypy.tree.graft(WSGIGateway(services), "/gateway/") cherrypy.quickstart(Root(), config=config)
Here is a suitable crossdomain.xml file.
Now run the script to start the web server. To test the gateway you can use a Python AMF client like this:
from pyamf.remoting.client import RemotingService gw = RemotingService('http://localhost:8080/gateway/') service = gw.getService('myservice') print service.echo('Hello World!')
You can easily expose more functions by adding them to the dictionary given to WSGIGateway. You can also create a totally different controller and expose it under another gateway URL.
