Pythonを使う
Pythonでは、オープンソースのAMF実装である「PyAMF」を使うことにより、サーバサイドとFlex・Flashの連携が可能になります。PyAMFの現時点での最新版は、2008年5月4日にリリースされた0.3.1です。PyAMFは、PylonsやTurboGears等の代表的なWebアプリケーションフレームワークと連携させることも出来ますが、今回は単独で起動するサービスを実装してみます。
PyAMFのインストール:
まずOS上にPythonをインストールした後、PyAMFをインストールします。http://download.pyamf.org/releases/から、PyAMF-0.3.1.zipを取得し、任意のディレクトリに展開した後、PyAMF-0.3.1ディレクトリに移動して以下のコマンドを実行します。
C:\PyAMF-0.3.1>python setup.py install
PyAMFでのサービスの実装:
サービスの実装は、普通に処理を記述するだけです。注意する点として、PyAMFではサービス名と、それを実装したメソッドとを関連付ける「services」プロパティを定義する必要があります。今回、サービス名は「hello」、実装したメソッドは「hello_service」ですので、
services = {
'hello': hello_service,
}
と定義します。
完全なソースコードは以下のようになります。
"""
Helloworld example server.
"""
import string
def hello(message):
"""
A simple helloworld client
"""
return "World! [message from client: " + string.upper(message) + "]"
services = {
'hello': hello_service,
}
if __name__ == '__main__':
from pyamf.remoting.gateway.wsgi import WSGIGateway
from wsgiref import simple_server
gw = WSGIGateway(services)
httpd = simple_server.WSGIServer(
('localhost', 3001),
simple_server.WSGIRequestHandler,
)
httpd.set_app(gw)
print "Running Helloworld AMF gateway on http://localhost:3001"
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
PyAMFでのサービスの起動:
コマンドプロンプトを開き、
C:\PyAMF-0.3.1> HelloWorldService.py
と入力して、サービスを起動します。実行時のイメージは以下のようになります。


