程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python XML-RPC implements a simple remote call process

編輯:Python

0  Preface


RPC  yes  Remote Procedure Call  Abbreviation , Translated into Chinese : Remote method call .
stay  Python  in , We can use  XML-RPC  Protocols create their own cross platform , Language independent server .XML Remote Procedure Call, namely  XML  Remote method call .
XML-RPC  Published in 1998 year , It is a set that allows to run on different operating systems 、 The program implementation in different environments is based on  Internet  Specification of procedure call and a series of implementation . This remote procedure call uses  http  As a transport protocol ,XML  As the encoding format of transmission information .
XML-RPC  The definition of is kept as simple as possible , But it can also transmit 、 Handle 、 Return complex data structures .XML- RPC(http://www.xmlrpc.com) By  UserLand Software(UserLand Software) Of  Dave Winer  And  Microsoft  Co publish [2]. Later in the new features are constantly introduced under , This standard gradually evolved into today's  SOAP  agreement .
XML-RPC  Is a remote procedure call method , It uses  HTTP  Delivered  XML  As a carrier .  With it , The client can call methods with parameters on the remote server ( The server to  URI  name ) And get structured data .
The simple understanding is : Define data as  XML  Format , adopt  HTTP  Protocol for remote transmission .

1  The server

stay  Python  in ,xmlrpc  The module is a collection  XML-RPC  Server and client implementation module package .  These modules are :
  • xmlrpc.client
  • xmlrpc.server

We use  SimpleXMLRPCServer  To create  SimpleXMLRPCSERVER  example , And tell it to listen for incoming requests . Next , We define some functions as part of the service and register them , So that the server knows how to call it .
Create a  xmlrpc_server.py  file . In the following example , We use  SimpleXMLRPCServer  Instance creation server , And register some predefined and customized functions . Last , We put the server into an infinite loop to receive and respond to requests . 
from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler


class RequestHandler(SimpleXMLRPCRequestHandler):

 rpc_paths = ('/RPC2',)


with SimpleXMLRPCServer(('localhost', 9000), requestHandler=RequestHandler) as server:
 
 server.register_introspection_functions()
 # Register len() function;
 server.register_function(len)

 # Register a function under a different name
 @server.register_function(name='rmndr')
 def remainder_function(x, y):
 return x // y

 # Register a function under function.__name__.
 @server.register_function
 def modl(x, y):
 return x % y
 server.serve_forever()

XML-RPC  It allows us to easily construct a simple remote call service . All you need to do is create a server instance ,  By its way  register_function()  To register functions , Then use the method  serve_forever()  To start it .
Once the above server is started , You can call through the client program , The program can refer to functions and make function calls .

2  Running client

Create a  xmlrpc_client.py  file :import xmlrpc.client

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

print(s.len("Yuzhou1su"))
print(s.rmndr(1024,128))
print(s.modl(7,3))

# Print list of available methods
print(s.system.listMethods())

The following output can be seen in the terminal $ python xmlrpc_client.py 
9
8
1
['len', 'modl', 'rmndr', 'system.listMethods', 'system.methodHelp', 'system.methodSignature']
  • The first result : character string  Yuzhou1su  The length is  9
  • Second result :1024  Divide  128  The result of the division of is  8
  • The third result :7  Divide  3  The remainder is  1
  • The last line : Output system method

summary


XML-RPC  Of shortcoming :
  • Its performance is not high
  • Transmission of other data formats is not supported . as well as  XML-RPC  All data will be serialized into  XML  Format , So it will be slower than other methods
  • And it can't protect against maliciously constructed data .

XML-RPC  Of advantage :
  • Transmit complex data .
  • Through the encapsulation of program language , Realize the call of remote object .

however , The advantage of this simple coding is that many other programming languages can understand . Use XML-RPC  Words , The client program can adopt  Python  Other languages , You can also visit Service for .
Put aside  XML-RPC  Don't say the limitations of , If you need it fast but not perfect (quick and dirty) The way Implement a remote procedure call system , So learn about  XML-RPC  It's still worth it . Many times it's simple Our plan is good enough . in addition ,JSON-RPC  Its principle is similar .
  • xmlrpc --- XMLRPC  Server and client modules
  • adopt XML-RPC Realize simple remote call
  • xmlrpc.client — XML-RPC  Client class library


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved