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.
The following example shows how a single method is exposed in this way. Create a file called server.py with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import logging
import cherrypy
from pyamf.remoting.gateway.wsgi import WSGIGateway
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(levelname)-5.5s [%(name)s] %(message)s'
)
def echo(data):
"""
This is a function that we will expose.
"""
return data
class Root(object):
"""
This is the root controller for the rest of the website.
"""
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'
}
}
services = {
'myservice.echo': echo,
# Add other exposed functions here
}
gateway = WSGIGateway(services, logger=logging, debug=True)
# This is where we hook in the WSGIGateway
cherrypy.tree.graft(gateway, "/gateway/")
cherrypy.quickstart(Root(), config=config)
|
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.
Here is a suitable crossdomain.xml file:
1 2 3 4 5 6 7 8 | <?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<site-control permitted-cross-domain-policies="all"/>
<allow-access-from domain="*" secure="false"/>
<allow-http-request-headers-from domain="*" headers="*" secure="false"/>
</cross-domain-policy>
|
Now run the script to start the web server.
To test the gateway you can use a Python AMF client like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import logging
from pyamf.remoting.client import RemotingService
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(levelname)-5.5s [%(name)s] %(message)s'
)
path = 'http://localhost:8080/gateway/'
gw = RemotingService(path, logger=logging, debug=True)
service = gw.getService('myservice')
print service.echo('Hello World!')
|