Zope中使用XML-RPC
来源:互联网 发布:怎么做微信淘宝优惠群 编辑:程序博客网 时间:2024/06/01 08:55
How to use XML-RPC with Zope
How to use XML-RPC with Zope
It is well known that Zope speaks XML-RPC, however it has not until now been well known how to use XML-RPC with Zope.
Why use XML-RPC?
Because it's cool and allows sending structured data over HTTP, and most importantly because it is supported by other things besides Zope.
If you just want to exchange data between Zope processes, you might want to look into Zope's own RPC mechanismZPublisher.Client
.
Using Zope as an XML-RPC server
Every Zope object can respond to HTTP requests. This is Zope's object publishing philosophy. For example, a Folder will tell you the names of the items it contains when you call itsobjectIds
method. So if the Folder is located at:
http://www.example.com/Foo/Bar/MyFolder
you can request its contents by calling:
http://www.example.com/Foo/Bar/MyFolder/objectIds
XML-RPC support works the same way. You can send an XML-RPC request to call theobjectIds
method directly to the MyFolder object:
POST /Foo/Bar/MyFolder HTTP/1.0 Content-Type: text/xml Content-length: 95 <?xml version="1.0"?> <methodCall> <methodName>objectIds</methodName> <params/> </methodCall>
The results will be a list of contained object names.
All Zope objects are publishable and thus all are XML-RPC aware. There is no need to do anything special. In fact, Zope will encode your response so it is sufficient to return standard Python objects and Zope will marshal them into XML-RPC format.
XML-RPC and access control
Since XML-RPC runs over HTTP Zope still obeys authentication rules. This is one of Zope's great strengths--a simple and powerful security model. Zope carries this security model to XML-RPC. Your XML-RPC user agent should use basic authentication when accessing protected resources.
Fredrik Lundh's XML-RPC Python module doesn't come with support for sending requests with basic authentication, but it can easily be extended to do so.
Here's an example of how to do this that works for me:
import string, xmlrpclib, httplib from base64 import encodestring class BasicAuthTransport(xmlrpclib.Transport): def __init__(self, username=None, password=None): self.username=username self.password=password def request(self, host, handler, request_body): # issue XML-RPC request h = httplib.HTTP(host) h.putrequest("POST", handler) # required by HTTP/1.1 h.putheader("Host", host) # required by XML-RPC h.putheader("User-Agent", self.user_agent) h.putheader("Content-Type", "text/xml") h.putheader("Content-Length", str(len(request_body))) # basic auth if self.username is not None and self.password is not None: h.putheader("AUTHORIZATION", "Basic %s" % string.replace( encodestring("%s:%s" % (self.username, self.password)), "\012", "")) h.endheaders() if request_body: h.send(request_body) errcode, errmsg, headers = h.getreply() if errcode != 200: raise xmlrpclib.ProtocolError( host + handler, errcode, errmsg, headers ) return self.parse_response(h.getfile())
Using XML-RPC as a client
Zope doesn't provide support in DTML to use XML-RPC as a client, but that doesn't mean that it can't be done.
Fredrik Lundh's XML-RPC Python module comes with Zope and you can use this in your External Methods or Zope Products to use XML-RPC as a client.
Here's an example:
import xmlrpclib def getStateName(self, number): "Returns a state name given an integer state number" server_url="http://betty.userland.com" server=xmlrpclib.Server(server_url) return server.examples.getStateName(number)
To call this External Method you might want to create a form like this:
<form action="getStateName"> state number <input name="number:int"> <input type="submit" value="get name"> </form>
This example does not show it, but as always when writing code to access remote resources you need to take into account the possibility that the connection fail in one way or another, and it could take a very long time to get a response.
It would be an interesting project (hint, hint) to write an XML-RPC Method Zope Product that was smart about caching, etcetera. This would make XML-RPC available from DTML in a safe form.
- Zope中使用XML-RPC
- php5中XML-RPC函数的使用
- php5中XML-RPC函数的使用
- PHP中XML-RPC使用笔记
- 在Perl中使用xml-rpc的实例
- PHP中使用XML-RPC构造Web Service简单入门
- PHP中使用XML-RPC构造Web Service简单入门
- PHP中使用XML-RPC构造Web Service简单入门
- PHP中使用XML-RPC构造Web Service简单入门
- 在Perl中使用xml-rpc的实例
- 在Perl中使用xml-rpc的实例
- 在Perl中使用xml-rpc的实例
- 在Perl中使用xml-rpc的实例
- 在Perl中使用xml-rpc的实例
- PHP中使用XML-RPC构造Web Service简单入门
- PHP中使用XML-RPC构造Web Service简单入门
- PHP中使用XML-RPC构造Web Service简单入门
- PHP中使用XML-RPC构造WebService简单入门
- 织梦广告位的添加
- 闲扯
- 如何在MDI程序中把子窗体菜单合并到主窗体上
- linux进程调度浅析
- 探讨工作流能给公司带来的几点益处
- Zope中使用XML-RPC
- bootstrap使用笔记
- java 子类父类同名变量的隐藏
- 迅雷看看全国第一家支持H.265
- 牛人林达华推荐有关机器学习的数学书籍
- (原创)斗牛游戏 初版
- 应用MIME将ZPT内容输出为excel文件(word文件)
- android 数据插入异常
- 第7集 构造函数中抛出的异常