< Python >xmlrpclib

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 xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer

def is_even(n):
    return n%2 == 0

server = 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 xmlrpclib

proxy = 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
>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值