Creating an XML-RPC Server


import SimpleXMLRPCServer serv = SimpleXMLRPCServer.SimpleXMLRPCServer(servAddr) serv.register_function(areaSquare) serv.register_introspection_functions() serv.serve_forever()

The SimpleXMLRPCServer module provided with Python allows you to implement web services that support the XML-RPC protocol for remote procedure calls or RPCs. The XML-RPC protocol uses XML data encoding to transmit remote procedure calls across the HTTP protocol. This section discusses how to use the SimpleXMLRPCServer module to create a simple XML-RPC server.

The first step is to create an XML-RPC server object by calling the SimpleXMLRPCServer(addr [, requestHandler [, logRequests]]) function of the SimpleXMLRPCServer module. The SimpleXMLRPCServer function accepts a list containing the address and port to use for the server and returns an XML-RPC server object. The requstHandler argument specifies a request handler object if needed, and the logRequests is a Boolean flag that specifies whether or not to log incoming requests.

After you have created the XML-RPC server object, register locally defined functions that will be provided remotely by calling the register_function(function) function of the XML-RPC server object.

After you have registered the local functions that will be provided remotely, register the introspection functions using the register_introspection_functions(function) function of the XML-RPC server object. The XML-RPC server supports the XML introspection API, which provides the system.listMethods(), system.methodHelp(), and system.MethodSignature() introspection functions. The register_introspection_functions() function registers those introspection functions so that they can be accessed by a remote client.

After you have registered the introspection functions, start the server using the serve_forever() function of the XML-RPC server object. The server will begin accepting remote procedure call requests from remote clients.

import SimpleXMLRPCServer servAddr = ("localhost", 8080) def areaSquare(length):     return length*length def areaRectangle(length, width):     return length*width def areaCircle(radius):     return 3.14*(radius*radius) serv = SimpleXMLRPCServer.SimpleXMLRPCServer(servAddr) #Register RPC functions serv.register_function(areaSquare) serv.register_function(areaRectangle) serv.register_function(areaCircle) #Register Introspective functions serv.register_introspection_functions() #Handle Requests serv.serve_forever()


xml-rpc_serv.py




Python Phrasebook(c) Essential Code and Commands
Python Phrasebook
ISBN: 0672329107
EAN: 2147483647
Year: N/A
Pages: 138
Authors: Brad Dayley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net