If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

Installing pyamf

First of all, you must install pyamf and make it visible to web2py (which initially comes without pyamf). For this purpose, visit pyamf download page and get ZIP of the latest stable release. Unzip it and install according to instructions in INSTALL.txt. I suggest using

python setup.py install --disable-ext

in order to avoid possible problems. It will place .egg package (something like PyAMF-0.5.1-py2.6.egg) in Python's installation folder under \Lib\site-packages (for example, *C:\Python26\Lib\site-packages). .egg is basically a zip archive (like .jar to Java), so open it and extract pyamf folder. Go to the web2py installation folder and find library.zip archive. Add pyamf to this archive. That's it. Now web2py will run pyamf transparently to you.

Python code

Let's assume you are developing an application called myapp and the web2py server runs on the localhost ( Add new controller called rpc.py. Add the following code to the controller:

 from gluon.tools import Service
    service = Service(globals())

    def call():
        return service()

    def test():
        return "Test!!!"

Note, mydomain is important, don't forget it!

Flex mxml/AS3 code

Create new Flex application and replace its content with the following code:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">    
                import mx.rpc.events.FaultEvent;
                import mx.rpc.events.ResultEvent;
                import mx.controls.Alert;

                private function resultHandler(event:ResultEvent):void

                private function faultHandler(event:FaultEvent):void

            <mx:method name="test" result="resultHandler(event)" fault="faultHandler(event)"/>
        <mx:Button x="250" y="150" label="Fire" click="amfService.test();"/>

Pay attention to the definition of the RemoteObject. Endpoint is a service URL. It doesn't include rpc method name, which should be specified in the name attribute of the mx:method object. /call/amfrpc3 is standard URL suffix, and it shouldn't be altered. It is important to specify destination attribute - it's the same id that appears in controller in @service.amfrpc3(...) decorator.


Note that in order for Flex to be able to invoke rpc service from different domain, one need to expose appropriate crossdomain.xml file at the top level of the server of that domain. For example:


In order to do this, create crossdomain.xml inside static folder of the application (web2py doesn't support public resources, so we will do some routing) and add appropriate access policy, for example full access (not desirable for security reasons):

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
    <allow-access-from domain="*" />

Now go to web2py installation folder and create routes.py file with the following content:

routes_in = (('/crossdomain.xml', '/myapp/static/crossdomain.xml'),)

This file instructs web2py server to redirect all requests to crossdomain.xml to its location at application's static resources. Don't forget to close and run the process of the server in order for it to reload the routing information.

Comments (0)

Hosting graciously provided by:
Python Anywhere