PyBurlap--Burlap协议的Python实现

缘起

老麦的Amowa终于有了一个实现:Buffalohttp://www.amowa.net/buffalo,而且客户端封装的确实不错,整体走了一遍他的JS和JAVA代码,越觉得有意思,尤其核心的Burlaphttp://www.caucho.com/resin-3.0/protocols/burlap.xtp协议,感觉确实比Xmlrpc来的简洁,上网找了找,还没有Python的实现,OK,那就自己写一个PyBurlap吧,我相信肯定比JAVA来的简洁方便,在配上老麦封装的JS端,肯定会让使用上更加方便:)

目前版本以及实现的功能

version:0.1

可以完成对string,int,long,double,list,map,base64,boolean,date,null这些数据类型进行交换

尚未实现的数据类型:xml

有关Burlap协议

http://www.caucho.com/resin-3.0/protocols/burlap.xtp

PyBurlap调用方法

代码足以说明一切使用方法了

if __name__ == "__main__":
    proxy = Burlap("http://localhost:8080/buffalo/Hello")

    try:
        print proxy.hello("Breeze")
        print proxy.replyInt(10)
        print proxy.replyLong(10000)
        print proxy.replyDouble(1000000.90)
        print proxy.callIntStr(100,"Breeze")

        print proxy.replyList(['Breeze','QingFeng','Wind',100,300.68])#list

        print proxy.replyMap({'Breeze':1,'QingFeng':2,'Wind':3,100:4,300.68:5})#map->Python is dict

        print proxy.replyBoolean(True)

        #ISO_8609_DATETIME = '%Y%m%dT%H%M%SZ'
        print proxy.replyDate(Date('20050501T095231Z'))

        import base64
        print proxy.replyBase64(base64.encodestring('Breeze Base64'))
    except Error, v:
        print "ERROR", v

调用结果如下:

Hello Breeze
10
10000
1000000.9
Breeze100
[u'Breeze', u'QingFeng', u'Wind', u'100', u'300.68', u'End Object']
{u'1': u'Breeze', u'0': u'End Map Object', u'3': u'Wind', u'2': u'QingFeng', u'5': u'300.68', u'4': u'100'}
1
20050501T095231.000Z
Breeze Base64

xmlrpc以及burlap这些东东的基本原理

其实就是Http协议的POST操作,废话不多说,代码如下:

import httplib

str = "<burlap:call>"
str += " <method>hello</method>"
str += " <string>China</string>"
str += "</burlap:call>"

http = httplib.HTTP('localhost:8080')
http.putrequest('POST','/buffalo/Hello')
http.putheader('User-Agent','PyBurlap by Wind')
http.putheader('Host','localhost:8080')
http.putheader("Content-Type", "text/xml")
http.endheaders()
http.send(str)
errcode, errmsg, headers = http.getreply()
print errcode
print http.getfile().read()

用Java实现的Burlap协议操作

OK,开始第一步,搭建环境 打开尘封以久的Eclipse,建立Burlap的服务器端环境,首先写一个简单的接口IHello

public interface IHello
{
    public abstract String hello();
}

然后是他的实现

public class HelloService extends BurlapServlet implements IHello
{
    public String hello()
    {
        System.out.println("Hello");
        return "Hello World!";
    }
}

HelloService当作一个普通的Servlet配置一下,然后把Tomcat跑起来

客户端调用

public class Client
{
    public static void main(String[] args)
    {
        String url = "http://localhost:8080/buffalo/Hello";;

        BurlapProxyFactory factory = new BurlapProxyFactory();
        IHello basic = null;
        try
        {
            basic = (IHello) factory.create(IHello.class, url);
        }
        catch (MalformedURLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("now(): " + basic.hello());
    }
}

以上的代码基本也就是按照Burlap的文档删删改改。 调用后效果如下: 此主题相关图片

http://www.faridea.com/bbs/images/upload/2005/04/21/233055.gif

OK,现在我们尝试在Python中调用这个HelloService,也不用开什么特定的编辑器,只要打开Python的脚本环境即可,然后输入如下命令。 此主题相关图片

http://www.faridea.com/bbs/images/upload/2005/04/21/233125.gif

我们看到在http.getfile.read()之后得到了正确的反馈: '<burlap:reply><string>Hello World!</string></burlap:reply>'

PyBurlap调用方法如下:

proxy = Burlap("http://localhost:8080/buffalo/Hello")#burlap服务的地址
print proxy.hello("Breeze")#调用远程方法hello(str)

调用后,成功返回Hello Breeze

写程序不仅仅是工作,在我看来他更是一种生活方式

联系方式

Gmail

gmail.png

MSN

hotmail.png

Blog

http://qingfeng.ushared.com/blog/

模仿ZoomQuiet也建立一个联系方式:)

下载

PyBurlap0.1.zip

反馈


CategoryCategory