解决as对象与python对象通过socket互换问题
来源:互联网 发布:淘宝网高达模型 编辑:程序博客网 时间:2024/06/04 19:38
使用pyamf,通过socket通信,as端,通过flash.net.Socket对象writeObject方法可以将复杂的as对象传过服务器python端
pyamf中的例子代码如下:
class TimerProtocol(Protocol):
interval = 1.0 # interval in seconds to send the time
encoding = pyamf.AMF0
timeout = 300
def __init__(self):
self.started = False
self.encoder = pyamf.get_encoder(self.encoding)
self.stream = self.encoder.stream
def dataReceived(self, data):
data = data.strip()
if data == 'start':
# start sending a date object that contains the current time
if not self.started:
self.start()
....
....
dataReceived方法中data变量只能接收到字符串变量,对于自定义的as对象也转变为了字符串,
将TimerProtocol继承LineReceiver,采用 def rawDataReceived(self, data)方法,收到的原始数据也是字符串的
如何转为python对象呢?
python对象转到as端,可以用 self.encoder.writeElement(python对象)的方式成功传输,就是不清楚如何接收自定义as对象
pyamf中的例子代码如下:
class TimerProtocol(Protocol):
interval = 1.0 # interval in seconds to send the time
encoding = pyamf.AMF0
timeout = 300
def __init__(self):
self.started = False
self.encoder = pyamf.get_encoder(self.encoding)
self.stream = self.encoder.stream
def dataReceived(self, data):
data = data.strip()
if data == 'start':
# start sending a date object that contains the current time
if not self.started:
self.start()
....
....
dataReceived方法中data变量只能接收到字符串变量,对于自定义的as对象也转变为了字符串,
将TimerProtocol继承LineReceiver,采用 def rawDataReceived(self, data)方法,收到的原始数据也是字符串的
如何转为python对象呢?
python对象转到as端,可以用 self.encoder.writeElement(python对象)的方式成功传输,就是不清楚如何接收自定义as对象
解决过程:
通过google code search 找到 http://wiidiaplayer.googlecode.com/svn中有类似代码
整理后,dataReceived写法如下:
def dataReceived(self, data):
input = pyamf.util.BufferedByteStream(data)
decoder = pyamf.decode(input,self.encoding)
asobject=decoder.next()
print type(asobject)
----------------------------------------------------------------------
print type(asobject)输出为 <class 'pyamf.ASObject'>
说明自定义as对象传过来为 pyamf.ASObject对象
通过源码了解ASObject继承自dict ,故可通过 asobject.get("属性名") 访问自定义对象的属性
整理后,dataReceived写法如下:
def dataReceived(self, data):
input = pyamf.util.BufferedByteStream(data)
decoder = pyamf.decode(input,self.encoding)
asobject=decoder.next()
print type(asobject)
----------------------------------------------------------------------
print type(asobject)输出为 <class 'pyamf.ASObject'>
说明自定义as对象传过来为 pyamf.ASObject对象
通过源码了解ASObject继承自dict ,故可通过 asobject.get("属性名") 访问自定义对象的属性
补充:
encodeData = pyamf.encode({'name':'hello','data':7876})
for obj in pyamf.decode(encodeData):
print obj
for obj in pyamf.decode(encodeData):
print obj
- 解决as对象与python对象通过socket互换问题
- jQuery对象与DOM对象互换,解决框架间冲突
- 对象与XML的互换
- dom对象与jQuery对象的互换
- jquery对象与dom对象互换
- java对象与JSON对象的互换
- JSON字符串与JSON对象互换
- C#(服务器)与Java(客户端)通过Socket传递对象
- jQuery对象与DOM对象区别及互换
- 尚未解决的问题---------------socket对象都有哪些内容????
- android 使用socket与pc传递对象的问题
- flash socket 发对象问题
- jQuery中json对象与json字符串互换
- jQuery中json对象与json字符串互换 (2
- jQuery中json对象与json字符串互换
- jQuery中json对象与json字符串互换
- jQuery中json对象与json字符串互换
- jQuery中json对象与json字符串互换
- [Templates That Are Usually Used]Header Files Include and Pre-Process(ToBeContinued)
- 黑马程序员_API--- java.lang.System
- 25个免费的数据可视化
- 使用twisted编写异步服务器
- .NET资源下载
- 解决as对象与python对象通过socket互换问题
- UIImageView绕着一个点旋转
- 20世界十大算法
- Server Error in '/fdc' Application错误解决办法
- 2013年1季度中国汽车车型动力满意度研究报告 ——合资A+级车
- 作业
- 用twisted创建tcp socket长链接amf server服务
- 杭电2048
- C++中new与delete问题学习