Python principles FOR XML-RPC __python

Source: Internet
Author: User
Tags pow

Transferred from http://www.cnblogs.com/wanghaoran/p/3189017.html

The Simplexmlrpcserver module provides a basic framework for XML-RPC service-side writes. You can use the Simplexmlrpcserver server to either remain idle or use Cgixmlrpcrequesthandler to embed in a CGI environment.

XML-RPC (remote Procedure Call) is an HTTP transport protocol that uses the remote program called methods in the format of the HTML. The client can invoke the method of the server-side with parameters and get the returned structure data. (The name of the service side is a URI). This module supports writing to XML-RPC-side code. It is used to handle the details of all transformations between the consolidated Python object and the XML message.

EG1 Service-side code:

From Simplexmlrpcserver import simplexmlrpcserver from
simplexmlrpcserver import Simplexmlrpcrequesthandler

Class RequestHandler (Simplexmlrpcrequesthandler):
    rpc_paths = ('/RPC2 ')

server = Simplexmlrpcserver ((" localhost ", 8000), RequestHandler = RequestHandler)
#server = Simplexmlrpcserver ((" localhost ", 8000)
Server.register_introspection_functions ()

server.register_function (POW)

def adder_function (x,y):
    return x+y
server.register_function (adder_function, ' Add ')

class Myfuncs:
    def div (self, x, y):
        return x//y
server.register_instance (Myfuncs ())
Server.serve_forever ()

EG1 Client code:

Import Xmlrpclib

s = xmlrpclib. Serverproxy (' http://localhost:8000 ')
print s.pow (2,3) print s.add (2,3) print
s.div (5,2)

Print S.system.listmethods ()

XML_RPC service-side creation steps:
1, Import simplexmlrpcserver module
2. Instantiate an XML-RPC service object and listen for requests on the specified port
Server = Simplexmlrpcserver (("localhost", 8000)
3. Define the function and register the function to the server side
Server.register_function (adder_function, ' Add ') # #adder_function是服务点定义的函数, add is the function that is used when the client calls.
Server.register_introspection_functions () # #如果用到内部函数, the internal function needs to be registered to the server.
Server.register_instance (Myfuncs ()) # #如果要注册的是一个类, you can use this function to register the methods in the class all to the server side.
4, the service side starts listens to run Server.serve_forever ()
XML_RPC Client Creation steps:
1, Import xmlrpclib Library module
2, create a proxy to the service side
Proxy = Xmlrpclib. Serverproxy (' http://localhost:8000 ') # #函数参数是URL格式
3, through the proxy can invoke the method of the service side.

Eg2 Service-side code:

Import datetime from
simplexmlrpcserver import simplexmlrpcserver
import Xmlrpclib

def today ():
    Today = Datetime.datetime.today () return
    xmlrpclib. DateTime (today)
def Add (x,y): Return
    x+y
def subtract (x,y): return
    X-y
def multiply (x,y):
    return x*y
def divide (x,y): Return
    x/y

server = Simplexmlrpcserver (("localhost", 8001))
print " Listening on port 8001 ... "
Server.register_multicall_functions ()
server.register_function (today, today)
Server.register_ function (Add, "Add")
server.register_function (Subtract, "subtract")
server.register_function (multiply, "Multiply")
server.register_function (Divide, "divide")
server.serve_forever ()

Attention:
Server.register_multicall_functions () # #这个函数的作用是可以使客户端同时调用服务端的的多个函数.

EG2 Client code:

Import xmlrpclib
import datetime

proxy = Xmlrpclib. Serverproxy ("http://localhost:8001/") print

proxy.add (7,3) print
proxy.subtract (7,3)
Print Proxy.multiply (7,3)
print proxy.divide (7,3) Today
= Proxy.today () multicall
= Xmlrpclib. MultiCall (proxy)
multicall.today ()
multicall.add (7,3)
multicall.subtract (7,3)
Multicall.multiply (7,3)
multicall.divide (7,3) Result
= MultiCall ()

# Convert the ISO8601 string to a DateTime object
converted = Datetime.datetime.strptime (Today.value, "%y%m%dt%h:%m:%s")
print "Today:%s"% Converted.strftime ("%d.%m.%y,%h:%m")
print "Today1:%s, 7+3=%d, 7-3=%d, 7*3=%d, 7/3=%d"% tuple (result)

Attention:
MultiCall = Xmlrpclib. MultiCall (proxy)
......
result = MultiCall () # #将这两个函数之间的返回值写入到result中.

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.