`
Shelly.Kuang
  • 浏览: 158164 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

XML-RPC How To

    博客分类:
  • Zope
阅读更多

XML-RPC How To

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 mechanism ZPublisher.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 its objectIds 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 the objectIds 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.

 

原地址:http://www.zope.org/Members/Amos/XML-RPC

分享到:
评论

相关推荐

    XML-RPC-HOWTO

    描述xml rpc的文档。做XML RPC时可进行参考。里面有比较详细地说明,以及一些实例。

    RemObjects SDK Documents mht

    RO40 - Support for XML-RPC RO41 - Update standard .NET event... RO42 - Introduction to Free Pascal... RO45 - How to Write a RemObjects SDK... RO46 - How to Write a RemObjects SDK... WP02 - Cross ...

    整合PHP和XML

    Learn how to use SAX, XSLT, and XPath to manipulate XML documents, as well as use of XML-RPC protocol for accessing procedures on a remote computer, and much more

    PHP Web 2.0 Mashup Projects.pdf

    we cover two basic web services to get our feet wet — XML-RPC and REST. The Internet UPC database is an XML-RPC-based service, while Amazon uses REST. Preface [ 2 ] We will create code to call XML-...

    Python Network Programming Cookbook, 2nd Edition - 2017

    Chapter 7, Working with Web Services – XML-RPC, SOAP, and REST, introduces you to various API protocols such as XML-RPC, SOAP, and REST. You can programmatically ask any website or web service for ...

    XML Processing with Perl, Python, and PHP (2002).pdf

    Think about what you could do if only you knew how to tell Perl to convert your XML- based documents into Word documents for editing, or to HTML for viewing on the Web, or to SQL tables for storage in...

    Enterprise Rails

    * Explore service-oriented architecture and web services with XML-RPC and REST * See how caching can be a dependable way to improve performance Building for scale requires more work up front, but ...

    Beginning Python (2005).pdf

    The Peer-to-Peer Architecture 354 Summary 354 Exercises 354 Chapter 17: Extension Programming with C 355 Extension Module Outline 356 Building and Installing Extension Modules 358 Passing ...

    GWT in Action

    Chapter 10 takes you into the world of GWT-RPC, where you’ll learn how to pass Java objects between the web browser and your Java servlets. ABOUT THIS BOOK xxvii Chapter 11 expands on the previous ...

    Aria2135032bit.7z

    JSON-RPC (over HTTP and WebSocket)/XML-RPC interface Run as a daemon process Selective download in multi-file torrent/Metalink Chunk checksum validation in Metalink Can disable segmented ...

    Python Cookbook, 2nd Edition

    How to Contact Us Safari® Enabled Acknowledgments Chapter 1. Text Introduction Recipe 1.1. Processing a String One Character at a Time Recipe 1.2. Converting Between Characters and ...

    PHP.Web.Services.APIs.for.the.Modern.Web.2nd.Edition

    PHP is ideally suited for both consuming and creating web services. You’ll learn how to use this language with JSON, XML, and other web service technologies. The second edition has been updated to ...

    [Go语言入门(含源码)] The Way to Go (with source code)

    The Way to Go,: A Thorough Introduction to the Go Programming Language 英文书籍,已Cross the wall,从Google获得书中源代码,分享一下。喜欢请购买正版。 目录如下: Contents Preface......................

    Learning Penetration Testing with Python(PACKT,2015)

    Use the Metasploit Remote Procedure Call (MSFRPC) to automate exploit generation and execution Use Python’s Scrapy, network, socket, office, Nmap libraries, and custom modules Parse Microsoft Office ...

    WordPress 宝典.pdf

    Learn how to use custom plugins and themes, retrieve data, maintain security, use social media, and modify your blog without changing any core code. You'll even get to know the ecosystem of products ...

    PHP5 完整官方 中文教程

    PDO Driver How-To Extension FAQs Zend Engine 2 API reference Zend Engine 1 The future: PHP 6 and Zend Engine 3 FAQ — FAQ:常见问题 一般信息 邮件列表 获取 PHP 数据库问题 安装 — 安装常见问题 编译问题 ...

    PHP5中文参考手册

    PDO Driver How-To Extension FAQs Zend Engine 2 API reference Zend Engine 1 The future: PHP 6 and Zend Engine 3 FAQ — FAQ:常见问题 一般信息 邮件列表 获取 PHP 数据库问题 安装 — 安装常见问题 编译问题 ...

    php.ini-development

    TTL for user-defined php.ini files (time-to-live) in seconds. Default is 300 seconds (5 minutes) ;user_ini.cache_ttl = 300 ;;;;;;;;;;;;;;;;;;;; ; Language Options ; ;;;;;;;;;;;;;;;;;;;; ; Enable ...

    php帮助文档,php。chm,php必备的中文手册

    45. PDO Driver How-To 46. Zend API:深入 PHP 内核 47. 扩展 PHP 3 VIII. FAQ:常见问题 48. 一般信息 49. 邮件列表 50. 获取 PHP 51. 数据库问题 52. 安装常见问题 53. 编译问题 54. 使用 PHP 55. PHP 和 HTML 56...

    ZendFramework中文文档

    10.8.5. Fetching a Rowset via a Many-to-many Relationship 10.8.6. Cascading Write Operations 10.8.6.1. Notes Regarding Cascading Operations 11. Zend_Debug 11.1. 输出变量的值 (Dumping Variables) 12...

Global site tag (gtag.js) - Google Analytics