foolscap实现rpc(三)
来源:互联网 发布:美国亚马逊数据分析 编辑:程序博客网 时间:2024/05/20 05:25
完整的实例
这里有2个程序,一个实现了server端的远程加法协议,另外的一个作为客户端。第一个例子使用的是unauthenticated Tub,所以不需要手动从服务端拷贝FURL到客户端。2个程序都是单独的程序,但一般来讲,你应该创建一个应用并传递文件到twistd-noy。下面展示的是没有authenticated的例子:
listings/pblserver.py服务器程序:
from twisted.internet import reactor
from foolscap.api import Referenceable, UnauthenticatedTub
class MathServer(Referenceable):
def remote_add(self, a, b):
return a+b
def remote_subtract(self, a, b):
return a-b
myserver = MathServer()
tub = UnauthenticatedTub()
tub.listenOn(“tcp:12345”)
tub.setLocation(“localhost:12345”)
url = tub.registerReference(myserver, “math-service”)
print “the object is available at:”, url
tub.startService()
reactor.run()
[listings/pblclient.py]客户端程序:
from twisted.internet import reactor
from foolscap.api import UnauthenticatedTub
def gotErr1(why):
print “unable to get the RemoteReference:”, why
reactor.stop()
def gotErr2(why):
print”unable to invoke the remote method:”, why
reactor.stop()
def gotReference(remote):
print “got a RemoteReference”
print “asking it to add 1+2”
d = remote.callRemote(“add”, a=1, b=2)
d .addCallbacks(gotAnswer, gotErr2)
def gotAnswer(answer):
print “the answer is”, answer
reactor.stop()
tub = UnauthenticatedTub()
tub.startService()
d = tub.getReference(“pbu://localhost:12345/math-service”)
d.addCallbacks(gotReference, gotErr1)
reactor.run()
调试问题:
1) 由于目标计算机积极拒绝
Ok,是由于以下的原因:
将tub.listenOn(“tcp:12345”)写成tub.listenOn(“tcp:1234”)导致该错误
下面的是authenticated例子:
listings/pb2server.py服务器程序:
from twisted.internet import reactor
from foolscap import crypto
from foolscap.api import Referenceable, Tub
class MathServer(Referenceable):
def remote_add(self, a, b):
return a+b
def remote_subtract(self, a, b):
return a-b
myserver = MathServer()
tub = Tub(certFile=”pb2server.pem”)
tub.listenOn(“tcp:12345”)
tub.setLocation(“localhost:12345”)
url = tub.registerReference(myserver, “math-service”)
print “the object is available at:”, url
tub.startService()
reactor.run()
[listings/pb2client.py]客户端程序:
import sys
from twisted.internet import reactor
from foolscap.api import Tub
def gotErr1(why):
print “unable to get the RemoteReference:”, why
reactor.stop()
def gotErr2(why):
print”unable to invoke the remote method:”, why
reactor.stop()
def gotReference(remote):
print “got a RemoteReference”
print “asking it to add 1+2”
d = remote.callRemote(“add”, a=1, b=2)
d .addCallbacks(gotAnswer, gotErr2)
def gotAnswer(answer):
print “the answer is”, answer
reactor.stop()
if len(sys.argv) < 2:
print “Usage: pb2client.py URL”
sys.exit(1)
url = sys.argv[1]
tub = Tub()
tub.startService()
d = tub.getReference(url)
d.addCallbacks(gotReference, gotErr1)
reactor.run()
问题:
1)no module named OpenSSL
安装openssl:http://pypi.python.org/pypi/pyOpenSSL下载pyOpenSSL-0.13.winxp32-py2.7.exe (md5)(根据自己的版本来决定)
- foolscap实现rpc(三)
- foolscap实现rpc(二)
- foolscap实现rpc(一)
- 基于Netty的RPC简单框架实现(三):Kryo实现序列化
- 分布式文件系统KFS源码阅读与分析(三):RPC实现机制(MetaServer端)
- 三种主流的WebService实现方案(REST/SOAP/XML-RPC)简述及比较
- 三种主流的Web服务实现方案(REST+SOAP+XML-RPC)简述及比较
- 三种主流的Web服务实现方案(REST+SOAP+XML-RPC)简述及比较
- 三种主流的WebService实现方案(REST/SOAP/XML-RPC)简述及比较
- 三种主流的WebService实现方案(REST/SOAP/XML-RPC)简述及比较
- 三种主流的Web服务实现方案(REST+SOAP+XML-RPC)简述及比较
- RPC入门总结(三)RMI+Zookeeper实现远程调用框架
- Hadoop源代码分析(三)RPC
- WebService学习系列(三)------XML-RPC
- RPC实践(三)Hessian实践
- RPC入门(一):RPC java代码简单实现
- RPC服务框架(二) 简单RPC实现
- 【远程调用框架】如何实现一个简单的RPC框架(五)优化三:软负载中心设计与实现
- 改变SpringSource Tool suite中选中关键字背景颜色
- android内存泄漏问题
- 正则表达式30分钟入门
- Android 实现圆形转盘菜单(CirCleMenu)
- 不用循环从0计数到1000
- foolscap实现rpc(三)
- git学习总结
- 存储过程返回结果集
- Web中怎样获取Spring中的ApplicationContext
- ListView具有多种item布局——实现微信对话列
- 每天学点English Email(商务性结尾1)
- 一些待整理的信息
- 内存卡清空的文件数据怎么恢复
- Activiti介绍及视频教程