Python's signature dish xmlrpc

Source: Internet
Author: User
Tags pow rfc822

First, Introduction

To address the service of RPC on the 80 port of the system without affecting the Web service being executed, people have come up with the method of transmitting RPC packets with the HTTP protocol. The most convenient way to transfer RPC packets on an HTTP protocol that is almost exclusively for transmitting text is to encode the RPC packet into textual form-for example, an XML file.
XML-RPC (http://www.xml-rpc.com) is an RPC protocol specified by the United States Userland Company. It encodes the RPC information packets into XML and then transmits the packets over HTTP;
Simple to understand:
The data is defined in XML format and transmitted remotely through the HTTP protocol.

Second, the benefits

1. Transmission of complex data.
2. Implement the invocation of the remote object through the encapsulation of the program language.

Iii. XMLRPC application in Python
Server-side:
From simplexmlrpcserver Import *
#import Simplexmlrpcrequesthandler

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

Class Math:
def _listmethods (self):
return [' Add ', ' POW ']

   def _methodhelp (self,method):
       If method = = ' Add ':
           return "Add (2,3) =>5"
       Elif method = = ' POW ':
          return "POW (x,y[,z]) =>number"
       Else:
          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))
# Register_introspection_functions:registers The Xml-rpc introspection methods in the System namespace
Server.register_introspection_functions ()
Server.register_function (Div, "div")
Server.register_function (lambda x,y:x*y, ' multiply ')
Server.register_instance (Math ())
#serve_forever: Handle One request at a time until shutdown.
#Polls for shutdown every poll_interval seconds. Ignores
#self. Timeout. If you need to does periodic tasks, do them in
#another thread.
Server.serve_forever ()

Client:

Import Xmlrpclib
s = xmlrpclib. Serverproxy (' http://localhost:8000 ')
Print (S.system.listmethods ())

Print (S.pow (2,3))
Print (S.add (2,3))
Print (S.div (3,2))
Print (s.multiply (4,5))

Operating conditions:

[email protected] xmlrpc]$ python server.py

/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:37] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:37] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:37] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:37] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:37] "POST/RPC2 http/1.0" 200-


/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:45] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:45] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:45] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:45] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:45] "POST/RPC2 http/1.0" 200-


[email protected] xmlrpc]$ python client.py
[' Add ', ' div ', ' multiply ', ' pow ', ' system.listmethods ', ' system.methodhelp ', ' system.methodsignature ']
8
5
1
20
[email protected] xmlrpc]$ python client.py
[' Add ', ' div ', ' multiply ', ' pow ', ' system.listmethods ', ' system.methodhelp ', ' system.methodsignature ']
8
5
1
20
[Email protected] xmlrpc]$



Python's signature dish xmlrpc

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.