1. 下载并安装pyamf
。Adobe的flash和flex使用AMF(Action Message Formate)来进行应用程序和服务器之间的通信。AMF将通信消息序列化成一种非常紧密的二进制形式在网络上进行传输。pyamf用于支持python和flash或flex的客户端进行通信。将python的安装路径加入windows的环境变量,解压缩pyamf,进入其目录,执行python setup.py install --disable-ext,开始安装。
2. 在mod_python的基础上,配置好wsgi。WSGI是指Web Server Gateway Interface,它是为web服务器和应用程序或web框架之间交互定义的一组基于python语言的规范。pyamf框架和Apache交互是遵循wsgi规范的,所以mod_python和pyamf之间还需要架一个wsgi桥梁。下载WSGI gateway for mod_python
,一个python文件。
3. 建立你的web应用的根目录,例如C:\pyweb。解压缩wsgi.rar,把wsgi.py放入C:\pyweb下。再在目录下建立startup.py,内容如下:
# -*- coding: UTF-8 -*-
from pyamf.remoting.gateway.wsgi import WSGIGateway
import logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(levelname)-5.5s [%(name)s] %(message)s'
)
class EchoService(object):
"""
Provide a simple server for testing.
"""
def echo(self, data):
"""
Return data with chevrons surrounding it.
"""
return '<<%s>>' % data
services = {
'echo': EchoService(),
# Add other exposed functions here
}
application = WSGIGateway(services, logger=logging, debug=True)
请注意这里的文件名叫startup.py和文件中的变量名application
4. 为Apache访问C:\pyweb赋予权限。打开httpd.conf,添加如下Directory节点:
<Directory "C:/pyweb">
Order allow,deny
Allow from all
</Directory>
5. 配置虚拟主机VitualHost。如果不明白什么是虚拟主机,请参考Apache的官方文档http://httpd.apache.org/docs/2.2/vhosts/
。打开httpd.conf,拉到最下面,去掉Include conf/extra/httpd-vhosts.conf前面的注释。再打开extra/httpd-vhosts.conf文件,修改如下
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
ServerName www.xiaying.com
DocumentRoot "C:/pyweb"
CustomLog "logs/www.xiaying.com-access.log" combined
ErrorLog "logs/www.xiaying.com-error.log"
LogLevel warn
ServerSignature Off
# PyAMF gateway
<Location /flashservices/gateway>
SetHandler mod_python
PythonPath "['C:/pyweb']+sys.path"
PythonHandler wsgi
PythonOption wsgi.application startup::application
PythonOption SCRIPT_NAME /flashservices/gateway
# Only enable the options below when you're debugging the
# application. Turn them Off in a production environment.
PythonDebug On
PythonAutoReload On
</Location>
</VirtualHost>
这是一种基于域名的虚拟主机,当我们用www.xiaying.com这个域名访问的时候,就会被定向到C:/pyweb目录。配置域名,请打开system32/drivers/etc/hosts,添加一行127.0.0.1 www.xiaying.com。
/flashservices/gateway这个定义了PyAMF的gateway,即flex所谓的endpoint,远程调用的URL是:http://www.xiaying.com/flashservices/gateway。
PythonOption wsgi.application startup::application 其中wsgi:application是键名,在wsgi.py文件中可以找到,startup::application分别对应模块名和模块中的对象名。
6. 下面我们先简单建立一个python客户端测试一下。代码如下:
package {
[Bindable]
[RemoteClass(alias='com.project.Project')]
public class Project {
public var project_name:String;
public function Project() { }
}
}
[RemoteClass(alias='com.project.Project')],指定了远程的python类。com包下的project模块的Project类,继承自object