Zope支持Flash消息(AMF)
项目描述
在Zope 2中支持AMF/Flash
简介
此包允许您使用Flex通过Actionscript 2或Actionscript 3通过AMF0或AMF3从Flash查询Zope 2。
我们只是提供Zope层。底层已经使用PyAMF包(参见 http://pyamf.org)完成。
让我们编写一个简单的AMF视图,它回显各种类型的输入
>>> from Products.Five import BrowserView >>> from datetime import datetime >>> import elementtree.ElementTree as etree >>> class EchoView(BrowserView): ... ... def echoString(self, value): ... return "%s" % value ... ... def echoProtectedString(self, value): ... return "%s" % value ... ... def echoList(self, value): ... return list(value) ... ... def echoDict(self, value): ... return dict(value) ... ... def echoVoid(self, value): ... pass ... ... def echoTuple(self, value): ... return tuple(value) ... ... def echoParams(self, value1, value2): ... return "%s-%s" % (value1, value2) ... ... def echoDate(self): ... return datetime(2008, 11, 17, 11, 11) ... ... def echoXML(self, value): ... root = etree.Element("html") ... body = etree.SubElement(root, 'body') ... body.text = value ... return root
现在我们将它注册为一个Flash视图。目前我们只注册了文件夹对象的视图,并调用用户的默认文件夹
>>> from zope.configuration import xmlconfig >>> ignored = xmlconfig.string(""" ... <configure ... xmlns="http://namespaces.zope.org/zope" ... xmlns:browser="http://namespaces.zope.org/browser" ... xmlns:flash="http://namespaces.zope.org/flash" ... > ... ... <include package="z3c.amf" file="meta.zcml" /> ... <include package="Products.Five" file="meta.zcml" /> ... <include package="z3c.amf" /> ... ... <flash:view ... for="OFS.interfaces.IFolder" ... methods="echoString echoList echoDict echoVoid echoTuple ... echoDate echoXML echoParams" ... class="z3c.amf.README.EchoView" ... permission="zope.Public" ... /> ... ... <flash:view ... for="OFS.interfaces.IFolder" ... methods="echoProtectedString" ... class="z3c.amf.README.EchoView" ... permission="zope2.FlashAccess" ... /> ... ... </configure> ... """)
我们创建一些辅助函数。对于请求
>>> def createAMFRequest(target, body, username=None, password=None, multiParameters=False): ... envelope = remoting.Envelope(pyamf.AMF0, pyamf.ClientTypes.Flash9) ... if username is not None and password is not None: ... envelope.headers['Credentials'] = dict(userid=unicode(username), ... password=unicode(password)) ... if multiParameters: ... request = remoting.Request(target, body, envelope) ... else: ... request = remoting.Request(target, [body], envelope) ... envelope[u'/1'] = request ... amfRequest = remoting.encode(envelope) ... amfRequest.seek(0) ... return amfRequest.read()
对于响应
>>> import pyamf >>> from pyamf import remoting >>> def printAMFResponse(response): ... context = pyamf.amf0.Context ... requests = remoting.decode(response.body, context()) ... for name, value in requests.items(): ... print (name, value, type(value.body))
基本类型
字符串
>>> amfRequest = createAMFRequest(target='echoString', body='Hello World!') >>> amfRequest '\x00\x03\x00\x00\x00\x01\x00\nechoString\x00\x02/1\x00\x00\x00\x00\n\x00\x00\x00\x01\x02\x00\x0cHello World!' >>> response = http(r""" ... POST /test_folder_1_ HTTP/1.0 ... Content-Length: 102 ... Content-Type: application/x-amf ... ... %s""" % amfRequest) >>> printAMFResponse(response) (u'/1', <Response status=/onResult>Hello World!</Response>, <type 'unicode'>)
列表
>>> amfRequest = createAMFRequest(target='echoList', body=[u'Hé', u'Ho']) >>> response = http(r""" ... POST /test_folder_1_ HTTP/1.0 ... Content-Length: 102 ... Content-Type: application/x-amf ... ... %s""" % amfRequest) >>> printAMFResponse(response) (u'/1', <Response status=/onResult>[u'H\xc3\xa9', u'Ho']</Response>, <type 'list'>)
字典
>>> amfRequest = createAMFRequest(target='echoDict', ... body={'fruit': 'orange'}) >>> response = http(r""" ... POST /test_folder_1_ HTTP/1.0 ... Content-Length: 102 ... Content-Type: application/x-amf ... ... %s""" % amfRequest) >>> printAMFResponse(response) (u'/1', <Response status=/onResult>{u'fruit': u'orange'}</Response>, <class 'pyamf.ASObject'>)
无返回值
>>> amfRequest = createAMFRequest(target='echoVoid', body='Hello World!') >>> response = http(r""" ... POST /test_folder_1_ HTTP/1.0 ... Content-Length: 102 ... Content-Type: application/x-amf ... ... %s""" % amfRequest) >>> printAMFResponse(response) (u'/1', <Response status=/onResult>None</Response>, <type 'NoneType'>)
元组
>>> amfRequest = createAMFRequest(target='echoTuple', body=['foo', 'bar']) >>> response = http(r""" ... POST /test_folder_1_ HTTP/1.0 ... Content-Length: 102 ... Content-Type: application/x-amf ... ... %s""" % amfRequest) >>> printAMFResponse(response) (u'/1', <Response status=/onResult>[u'foo', u'bar']</Response>, <type 'list'>)
日期时间
>>> amfRequest = createAMFRequest(target='echoDate', body=None) >>> response = http(r""" ... POST /test_folder_1_ HTTP/1.0 ... Content-Length: 102 ... Content-Type: application/x-amf ... ... %s""" % amfRequest) >>> printAMFResponse(response) (u'/1', <Response status=/onResult>2008-11-17 11:11:00</Response>, <type 'datetime.datetime'>)
XML
>>> amfRequest = createAMFRequest(target='echoXML', body='It works!') >>> response = http(r""" ... POST /test_folder_1_ HTTP/1.0 ... Content-Length: 102 ... Content-Type: application/x-amf ... ... %s""" % amfRequest) >>> printAMFResponse(response) (u'/1', <Response status=/onResult><Element html at ...></Response>, <type 'instance'>)
多参数
>>> amfRequest = createAMFRequest(target='echoParams', body=['foo', 'bar'], ... multiParameters=True) >>> response = http(r""" ... POST /test_folder_1_ HTTP/1.0 ... Content-Length: 102 ... Content-Type: application/x-amf ... ... %s""" % amfRequest) >>> printAMFResponse(response) (u'/1', <Response status=/onResult>foo-bar</Response>, <type 'unicode'>)
错误
>>> amfRequest = createAMFRequest(target='echoUnknown', body=['foo', 'bar']) >>> response = http(r""" ... POST /test_folder_1_ HTTP/1.0 ... Content-Length: 102 ... Content-Type: application/x-amf ... ... %s""" % amfRequest) >>> printAMFResponse(response) (u'/1', <Response status=/onStatus><ErrorFault level=error code=NotFound type=Resource not found... ...
用户身份验证
尝试在Flash中访问我们的受保护视图而不提供登录/密码
>>> amfRequest = createAMFRequest(target='echoProtectedString', ... body='It works!') >>> response = http(r""" ... POST /test_folder_1_ HTTP/1.0 ... Content-Length: 102 ... Content-Type: application/x-amf ... ... %s""" % amfRequest) >>> printAMFResponse(response) (u'/1', <Response status=/onStatus><ErrorFault level=error code=zExceptions.unauthorized.Unauthorized type=Not authorized></Response>, <class 'pyamf.remoting.ErrorFault'>)
现在尝试使用登录/密码访问
>>> from Testing.ZopeTestCase import user_name, user_password >>> amfRequest = createAMFRequest(target='echoProtectedString', ... body="Hello World!", username=user_name, ... password=user_password) >>> response = http(r""" ... POST /test_folder_1_ HTTP/1.0 ... Content-Length: 200 ... Content-Type: application/x-amf ... ... %s""" % amfRequest) >>> printAMFResponse(response) (u'/1', <Response status=/onResult>Hello World!</Response>, <type 'unicode'>)
服务中的路径
>>> amfRequest = createAMFRequest(target='test_folder_1_.echoProtectedString', ... body='It works!', username=user_name, ... password=user_password) >>> response = http(r""" ... POST / HTTP/1.0 ... Content-Length: 200 ... Content-Type: application/x-amf ... ... %s""" % amfRequest) >>> printAMFResponse(response) (u'/1', <Response status=/onResult>It works!</Response>, <type 'unicode'>)
更改日志
0.2 - (2008-11-25)
处理服务中的路径更改
注册crossdomain.xml视图
0.1 - (2008-11-17)
初始版本
项目详情
关闭
z3c.amf-0.2.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 71717476062744dba6d8cbccb84804d3087922644c820a5fe2b2c4f8c798aa6a |
|
MD5 | 1f89e3e345c46c95e4bc7f0d47d1d903 |
|
BLAKE2b-256 | 7c0dc4e1409b33da70922732afdb22515894ac4ad2cb4e88f92bca2f879fea1c |