Python's signature dish xmlrpc

Source: Internet
Author: User
Tags pow rfc822

First, Brief introduction

In order to resolve the service that provides RPC in the system 80port. Without affecting the running Web service. People have come up with a way to transfer RPC packets using the HTTP protocol. For HTTP protocols that are almost exclusively for transmitting text. The RPC packet on which to transfer. The easiest way to do this is to encode the RPC packet into textual form-such as 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:
Defines the data as an XML format. Remote transmission over the HTTP protocol.

Second, the advantages

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 simplexmlrpcrequesthandlerdef div (x, y): return x-yclass math:def _listmetho DS (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 _di           Spatch (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)) # R Egister_introspection_functions:registers the Xml-rpc introspection methods in the system Namespaceserver.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 xmlrpclibs = 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))

Status of implementation:

[[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 Readheaderslocalhost.localdomain--[16/Nov/2014 23:25:37] "POST /RPC2 http/1.0 "$-/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders/usr/lib64/python2.6/ rfc822.py, Readheaders, 205, Exit Readheaderslocalhost.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 Readheaderslocalhost.localdomain--[16/nov/2014 23:25:37] "POST/RPC2 http/1.0"-/usr/lib64/python2.6/rfc822. PY, Readheaders, 131, enter readheaders/usr/lib64/python2.6/rfc822.py, Readheaders, 205, exit Readheaderslocalhost.localdomain--[16/nov/2014 23:25:37] "POST/RPC2 http/1.0"-/usr/lib64/python2.6/rfc822.py, R Eadheaders, 131, enter readheaders/usr/lib64/python2.6/rfc822.py, ReadheAders, 205, Exit Readheaderslocalhost.localdomain--[16/nov/2014 23:25:37] "POST/RPC2 http/1.0"-/usr/lib64/python 2.6/rfc822.py, Readheaders, 131, enter readheaders/usr/lib64/python2.6/rfc822.py, Readheaders, 205, exit Readheaderslocalhost.localdomain--[16/nov/2014 23:25:45] "POST/RPC2 http/1.0"-/usr/lib64/python2.6/rfc822.py, R Eadheaders, 131, enter readheaders/usr/lib64/python2.6/rfc822.py, Readheaders, 205, exit Readheaderslocalhost.localdomain--[16/nov/2014 23:25:45] "POST/RPC2 http/1.0"-/usr/lib64/python2.6/rfc822.py, R Eadheaders, 131, enter readheaders/usr/lib64/python2.6/rfc822.py, Readheaders, 205, exit Readheaderslocalhost.localdomain--[16/nov/2014 23:25:45] "POST/RPC2 http/1.0"-/usr/lib64/python2.6/rfc822.py, R Eadheaders, 131, enter readheaders/usr/lib64/python2.6/rfc822.py, Readheaders, 205, exit Readheaderslocalhost.localdomain--[16/nov/2014 23:25:45] "POST/RPC2 http/1.0"-/usr/lib64/python2.6/rfc822.py, R Eadheaders, 131, ENter readheaders/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaderslocalhost.localdomain--[16/Nov/ 23:25:45] "POST/RPC2 http/1.0"-[[email protected] xmlrpc]$ python client.py [' Add ', ' div ', ' multiply ', ' PO W ', ' system.listmethods ', ' system.methodhelp ', ' system.methodsignature ']85120[[email protected] xmlrpc]$ Python client.py [' Add ', ' div ', ' multiply ', ' pow ', ' system.listmethods ', ' system.methodhelp ', ' system.methodsignature '  ]85120[[email protected] xmlrpc]$


??

Python's signature dish xmlrpc

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.