Python XML RPC Server side and client instance _python

Source: Internet
Author: User
Tags pow xml rpc server

One, remote procedure call RPC

XML-RPC is-a-Remote Procedure call method, that uses, passed via HTTP as a transport. With it, a client can call methods with parameters on a remote server (the server was named by a URI) and get back Structur Ed data. This module supports writing XML-RPC client code; It handles all the details of translating between Conformable Python objects and XML on the wire.

Simply, the client can invoke the method provided on the server and then get the result of execution. Similar to the WebService.

Recommended to view XMLPRC source files: C:\Python31\Lib\xmlrpc

Second, the example

1) Server

Copy Code code as follows:

From Xmlrpc.server import Simplexmlrpcserver
From Xmlrpc.server import Simplexmlrpcrequesthandler

def div (x,y):
return x-y

Class Math:
def _listmethods (self):
# This method must is present for System.listmethods
# to work
return [' Add ', ' POW ']
def _methodhelp (Self, method):
# This method must is present for System.methodhelp
# to work
If method = = ' Add ':
Return "Add (2,3) => 5"
Elif method = = ' POW ':
Return "POW (x, y[, z]) => number"
Else
# By convention, return empty
# string If no help is available
Return ""
def _dispatch (self, Method, params):
If method = = ' POW ':
Return Pow (*params)
Elif method = = ' Add ':
return params[0] + params[1]
Else
Raise ' Bad method '

Server = Simplexmlrpcserver (("localhost", 8000)
Server.register_introspection_functions ()
Server.register_function (Div, "div")
Server.register_function (lambda x,y:x*y, ' multiply ')
Server.register_instance (Math ())
Server.serve_forever ()

2) Client

Copy Code code as follows:

Import Xmlrpc.client

s = xmlrpc.client.ServerProxy (' http://localhost:8000 ')

Print (S.system.listmethods ())

Print (S.pow (2,3)) # Returns 28
Print (S.add (2,3)) # Returns 5
Print (S.div (3,2)) # Returns 1
Print (s.multiply (4,5)) # Returns 20

3) Result

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.