# Pylons

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:

>      1
>      2
>      3
>      4
>      5
>      6
>      7
>      8
>      9
>     10
>     11
>     12
>     13
>     14
>     15
>     16
>     17
>     18
>     19
>     20
>     21

>     import logging

>     from testproject.lib import helpers as h

>     log = logging.getLogger(__name__)

>     def echo(data):
>         """
>         This is a function that we will expose.
>         """
>         # 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, logger=log, debug=True)

> 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', controller='gateway')

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

>     from pyamf.remoting.gateway.wsgi import WSGIGateway

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

>     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>

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:

>      1
>      2
>      3
>      4
>      5
>      6
>      7
>      8
>      9
>     10
>     11
>     12
>     13
>     14

>     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')
>     echo = service.echo('Hello World!')

>     logging.debug(echo)