Using Xmlrpc in Python (Getting started)

Source: Internet
Author: User

First, Introduction

RPC is the abbreviation for remote Procedure call, which is translated into Chinese as: long-distance method invocation.

It is a technique for invoking a process (method) on a remote machine on a local machine, which is also known as "distributed Computing" and is a technique invented to improve the "interoperability" of individual discrete machines.

The full name of XML-RPC is XML remote Procedure call, which is the XML remoting method invocation.

It is a set of specifications and a series of implementations that allow programs that run on different operating systems, in different environments, to implement Internet-based process calls. This remote procedure call uses HTTP as the transport protocol, and XML as the encoding format for transmitting information. The definition of XML-RPC is as simple as possible, but simultaneously capable of transmitting, processing, and returning complex data structures. XML-RPC (http://www.xml-rpc.com) is an RPC protocol specified by the United States Userland Company. The simple understanding is that the data is defined in XML format and transmitted remotely through 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.


third, Python under the Xml-rpc

1. Class Library: Simplexmlrpcserver

Generally used on the server side, this module is used to construct a most basic XML-RPC server framework.

2. Class Library: Xmlrpclib

Typically used on the client side, this module is used to invoke functions registered on the XML-RPC server, Xmlrpclib is not a type-safe module that cannot withstand maliciously constructed data, and some processing work needs to be given to the developer himself.

Approximate usage: Use the Simplexmlrpcserver module to run the XMLRPC server, where you register the functions or objects provided by the server, and then use Xmlrpclib within the client. Serverproxy connect to the server, you want to invoke the function of the server, call Serverproxy directly.

Simple example: Hello XMLPRC

Server side: xmlrpc_server.py

ImportSimplexmlrpcserverclassMyObject:defSayHello (self):return "Hello XMLPRC"obj =MyObject () server= Simplexmlrpcserver.simplexmlrpcserver (("localhost", 8088) ) server.register_instance (obj)Print "Listening on port 8088"Server.serve_forever ()

Client: xmlrpc_client.py

Import= xmlrpclib. Serverproxy ("http://localhost:8088"= Server.sayhello ()  Print"Result:" + words

Open a terminal, enter a command, run the server-side program:

chmod u+x xmlrpc_server.pyxmlrpc_server.py

Open another new terminal, enter a command, run the client program:
chmod u+x Xmlrpc_client.pypython xmlrpc_client.py

You can see the output from the client console:Hello XMLPRC.




Report:
Simplexmlrpcserver is a single-threaded server. This means that if several clients make multiple requests at the same time, the other requests must wait for the first request to complete before continuing.
If you modify the server side as follows:
 fromSimplexmlrpcserverImportSimplexmlrpcserver fromSocketserverImportThreadingMixInclassThreadxmlrpcserver (ThreadingMixIn, Simplexmlrpcserver):PassclassMyObject:defSayHello (self):return "Hello XMLPRC"obj=MyObject ()Server = Threadxmlrpcserver (("localhost", 8088), allow_none=True) server.register_instance (obj)Print "Listening on port 8088"Server.serve_forever ()

At this point, the server supports multi-threaded concurrency.

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.