Table of Contents

  1. Introduction
  2. Example

AMF Remoting gateway in Pylons with PyAMF

Introduction

The following tutorial describes how to set up a bare bones Pylons project with a gateway exposing a method.
Since Pylons supports generic WSGI apps as controllers, setting up a remoting gateway is trivial using the WSGI gateway.

Example

1. Create a new Pylons project with:

$ paster create -t pylons testproject


2. cd into it and create a controller:

$ cd testproject
$ paster controller gateway


3. Replace the contents of testproject/controllers/gateway.py with the following:

import logging

from testproject.lib.base import *

log = logging.getLogger(__name__)

# This is a function that we will expose
def echo(data):
   # print data to the console
   log.debug('Echo: %s', data)
   # echo data back to the client
   return data

services = {
   'myservice.echo': echo,
   # Add other exposed functions and classes here
}

GatewayController = h.WSGIGateway(services)

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.


4. Add the controller to the routing map, open testproject/config/routing.py and look for the line:

# CUSTOM ROUTES HERE

Just below that line, add a mapping to the controller you created earlier. This maps URLs with the prefix 'gateway' to the AMF gateway.

map.connect('gateway*path_info', controller='gateway')


5. Import the remoting gateway helper, open testproject/lib/helpers.py and add:

from pyamf.remoting.gateway.wsgi import WSGIGateway


6. Copy a crossdomain.xml file into testproject/public.


7. Fire up the web server with:

$ paster serve --reload development.ini

That should print something like:

Starting subprocess with file monitor
Starting server in PID 4247.
serving on 0.0.0.0:5000 view at http://127.0.0.1:5000


8. To test the gateway you can use a Python AMF client like this:

import logging
        
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s %(levelname)-5.5s [%(name)s] %(message)s'
)

from pyamf.remoting.client import RemotingService

client = RemotingService('http://127.0.0.1:5000/gateway')
service = client.getService('myservice')

print service.echo('Hello World!')