Try Python XML-RPC Remote Call

Source: Internet
Author: User
What is XML-RPC?
XML-RPC is the originator of XML Web Services. It is a simple specification for Remote Procedure Call (RPC), which uses HTTP as the transmission protocol and XML vocabulary as the message payload. Since the XML-RPC is very simple (the entire specification is printed out less than ten pages), it has become very popular and now most languages have standard or already available XML-RPC implementations. These languages include python, which began bundling xmlrpclib (XML-RPC implementation developed by Fredrik lundh) in version 2.2.

First, we plan to transform the CMS (context Manager System) system into python. The first thing is to make public version changes for remote calls.

Import simplexmlrpcserver

# Define your own CMS class
Class mycms:
Def getversion (Self): # Release Method
Return "powerd by Python 0.1a"

CMS = mycms ()
Server = simplexmlrpcserver. simplexmlrpcserver ("localhost", 8888 ))
Server. register_instance (CMS)

Print "listening on port 8888"
Server. serve_forever () # Run the command on the server and listen to port 8888.

After execution:
Images related to this topic

The client calls the code to obtain the latest version information.

Import xmlrpclib

Server = xmlrpclib. serverproxy ("http: // localhost: 8888"

Version = server. getversion ()

Print "version:" + version

After execution:
Images related to this topic

Summary:
Significantly less than the same Java implementation code, enabling more focus on the system itself
Java XMLRPC implementation: http://ws.apache.org/xmlrpc/
The Java call code is as follows:

Xmlrpcclient XMLRPC = NULL;
Try
{
XMLRPC = new xmlrpcclient ("http: // localhost: 8888 /";);
}
Catch (malformedurlexception E)
{
E. printstacktrace ();
}
Vector Params = new vector ();
Try
{
String result = (string) xmlrpc.exe cute ("getversion", Params );
System. Out. println (result );
}
Catch (xmlrpcexception E1)
{
E1.printstacktrace ();
}
Catch (ioexception E1)
{
E1.printstacktrace ();
}

Execution result
Images related to this topic

For more information, please go here:
Http://www.python.org/doc/current/lib/module-xmlrpclib.html
Http://www.python.org/doc/current/lib/module-SimpleXMLRPCServer.html

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.