< Python >xmlrpclib

来源:互联网 发布:java p2p 合肥学院 编辑:程序博客网 时间:2024/06/06 17:45

http://docs.python.org/2.7/library/xmlrpclib.html?highlight=serverproxy#xmlrpclib.ServerProxy

Python document  讲述了如何使用 Python版的 xmlrpclib,

Note

The xmlrpclib module has been renamed toxmlrpc.client inPython 3. The2to3 tool will automatically adapt imports whenconverting your sources to Python 3.

New in version 2.2.

Source code: Lib/xmlrpclib.py


XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as atransport. With it, a client can call methods with parameters on a remoteserver (the server is named by a URI) and get back structured data. This modulesupports writing XML-RPC client code; it handles all the details of translatingbetween conformable Python objects and XML on the wire.

classxmlrpclib.ServerProxy(uri[,transport[,encoding[,verbose[,allow_none[,use_datetime]]]]])

A ServerProxy instance is an object that manages communication with aremote XML-RPC server. The required first argument is a URI (Uniform ResourceIndicator), and will normally be the URL of the server. The optional secondargument is a transport factory instance; by default it is an internalSafeTransport instance for https: URLs and an internal HTTPTransport instance otherwise. The optional third argument is anencoding, by default UTF-8. The optional fourth argument is a debugging flag.Ifallow_none is true, the Python constant None will be translated intoXML; the default behaviour is forNone to raise aTypeError. This isa commonly-used extension to the XML-RPC specification, but isn’t supported byall clients and servers; seehttp://ontosys.com/xml-rpc/extensions.php for adescription. The use_datetime flag can be used to cause date/time values tobe presented asdatetime.datetime objects; this is false by default.datetime.datetime objects may be passed to calls.

The returned instance is a proxy object with methods that can be used to invokecorresponding RPC calls on the remote server. If the remote server supports theintrospection API, the proxy can also be used to query the remote server for themethods it supports (service discovery) and fetch other server-associatedmetadata.

20.23.1. ServerProxy Objects

A ServerProxy instance has a method corresponding to each remoteprocedure call accepted by the XML-RPC server. Calling the method performs anRPC, dispatched by both name and argument signature (e.g. the same method namecan be overloaded with multiple argument signatures). The RPC finishes byreturning a value, which may be either returned data in a conformant type or aFault or ProtocolError object indicating an error.

Servers that support the XML introspection API support some common methodsgrouped under the reservedsystem attribute:

ServerProxy.system.listMethods()

This method returns a list of strings, one for each (non-system) methodsupported by the XML-RPC server.

一个简单的例子:

A working example follows. The server code:

import xmlrpclibfrom SimpleXMLRPCServer import SimpleXMLRPCServerdef is_even(n):    return n%2 == 0server = SimpleXMLRPCServer(("localhost", 8000))print "Listening on port 8000..."server.register_function(is_even, "is_even")server.serve_forever()

The client code for the preceding server:

import xmlrpclibproxy = xmlrpclib.ServerProxy("http://localhost:8000/")print "3 is even: %s" % str(proxy.is_even(3))print "100 is even: %s" % str(proxy.is_even(100))


idle 1: server

>>> import xmlrpclib
>>> from SimpleXMLRPCServer import SimpleXMLRPCServer
>>> def is_even(n):
    return n%2 == 0

>>> server = SimpleXMLRPCServer(("localhost",8000))
>>> print "Listening on port 8000..."
Listening on port 8000...
>>> server.register_function(is_even, "is_even")
>>> server.serve_forever()
localhost - - [28/Aug/2013 16:42:48] "POST /RPC2 HTTP/1.1" 200 -
localhost - - [28/Aug/2013 16:42:55] "POST /RPC2 HTTP/1.1" 200 -
localhost - - [28/Aug/2013 16:43:23] "POST /RPC2 HTTP/1.1" 200 -


idle2: client

>>> import xmlrpclib
>>> proxy = xmlrpclib.ServerProxy("http://localhost:8000")
>>> print "3 is even: %s" % str(proxy.is_even(3))
3 is even: False
>>> print "100 is even: %s" % str(proxy.is_even(100))
100 is even: True
>>>

原创粉丝点击