用Python实现一个XML-RPC服务器

一个简单的Server

~~~~~~~~~~~~~~~~~~~~~~ server.py ~~~~~~~~~~~~~~~~~~~~~~
from SimpleXMLRPCServer import SimpleXMLRPCServer
import logging
import os
# Set up logging
logging.basicConfig(level=logging.DEBUG)
server = SimpleXMLRPCServer((’localhost’, 9000), logRequests=True)
# Expose a function
def list_contents(dir_name):
  logging.debug(’list_contents(%s)’, dir_name)
  return os.listdir(dir_name)

server.register_function(list_contents)
try:
  print ’Use Control-C to exit’
  server.serve_forever()
except KeyboardInterrupt:
print ’Exiting’

~~~~~~~~~~~~~~~~~~~~~~ client.py ~~~~~~~~~~~~~~~~~~~~~~
proxy = xmlrpclib.ServerProxy(’http://localhost:9000’)
print proxy.list_contents(’/tmp’)

别名

from SimpleXMLRPCServer import SimpleXMLRPCServer
import os
server = SimpleXMLRPCServer((’localhost’, 9000))
# Expose a function with an alternate name
def list_contents(dir_name):
  return os.listdir(dir_name)
server.register_function(list_contents, ’dir’)
try:
  print ’Use Control-C to exit’
  server.serve_forever()
except KeyboardInterrupt:
  print ’Exiting’

~~~~~~~~~~~~~~~~~~~~~~ client.py ~~~~~~~~~~~~~~~~~~~~~~
proxy = xmlrpclib.ServerProxy(’http://localhost:9000’)
print ’dir():’, proxy.dir(’/tmp’)
print ’list_contents():’, proxy.list_contents(’/tmp’)

Dotted Names

from SimpleXMLRPCServer import SimpleXMLRPCServer
import os

server = SimpleXMLRPCServer((’localhost’, 9000), allow_none=True)

server.register_function(os.listdir, ’dir.list’)
server.register_function(os.mkdir, ’dir.create’)
server.register_function(os.rmdir, ’dir.remove’)

try:
  print ’Use Control-C to exit’
  server.serve_forever()
except KeyboardInterrupt:
  print ’Exiting’

~~~~~~~~~~~~~~~~~~~~~~ client.py ~~~~~~~~~~~~~~~~~~~~~~
import xmlrpclib
proxy = xmlrpclib.ServerProxy(’http://localhost:9000’)
print ’BEFORE :’, ’EXAMPLE’ in proxy.dir.list(’/tmp’)
print ’CREATE :’, proxy.dir.create(’/tmp/EXAMPLE’)
print ’SHOULD EXIST :’, ’EXAMPLE’ in proxy.dir.list(’/tmp’)
print ’REMOVE :’, proxy.dir.remove(’/tmp/EXAMPLE’)
print ’AFTER :’, ’EXAMPLE’ in proxy.dir.list(’/tmp’)

任意的名字

from SimpleXMLRPCServer import SimpleXMLRPCServer

server = SimpleXMLRPCServer((’localhost’, 9000))

def my_function(a, b):
  return a * b
server.register_function(my_function, ’multiply args’)

try:
  print ’Use Control-C to exit’
  server.serve_forever()
except KeyboardInterrupt:
  print ’Exiting’

~~~~~~~~~~~~~~~~~~~~~~ client.py ~~~~~~~~~~~~~~~~~~~~~~
import xmlrpclib

proxy = xmlrpclib.ServerProxy(’http://localhost:9000’)
print getattr(proxy, ’multiply args’)(5, 5)

暴露对象的方法

By default, register_instance() finds all callable attributes of the instance with names not starting with ‘_‘and registers them with their name.

from SimpleXMLRPCServer import SimpleXMLRPCServer
import os
import inspect

server = SimpleXMLRPCServer((’localhost’, 9000), logRequests=True)

class DirectoryService:
  def list(self, dir_name):
    return os.listdir(dir_name)

server.register_instance(DirectoryService())

try:
  print ’Use Control-C to exit’
  server.serve_forever()
except KeyboardInterrupt:
  print ’Exiting’



~~~~~~~~~~~~~~~~~~~~~~ client.py ~~~~~~~~~~~~~~~~~~~~~~
import xmlrpclib
proxy = xmlrpclib.ServerProxy(’http://localhost:9000’)
print proxy.list(’/tmp’)

暴露对象的方法 + Dotted Names

from SimpleXMLRPCServer import SimpleXMLRPCServer
import os
import inspect

server = SimpleXMLRPCServer((’localhost’, 9000), logRequests=True)

class ServiceRoot:
  pass

class DirectoryService:
  def list(self, dir_name):
    return os.listdir(dir_name)

root = ServiceRoot()
root.dir = DirectoryService()

server.register_instance(DirectoryService())

try:
  print ’Use Control-C to exit’
  server.serve_forever()
except KeyboardInterrupt:
  print ’Exiting’



~~~~~~~~~~~~~~~~~~~~~~ client.py ~~~~~~~~~~~~~~~~~~~~~~
import xmlrpclib
proxy = xmlrpclib.ServerProxy(’http://localhost:9000’)
print proxy.dir.list(’/tmp’)

自定义转发调用

在客户端尝试调用MyService的一个方法时 _dispatch()方法会被调用。在这个方法里,我们加了一个方法的前缀限定,并且要求这个方法必须有一个叫exposed的属性并且值为True。当然你可以随意设计_dispatch的逻辑来满足你的转发需求。

from SimpleXMLRPCServer import SimpleXMLRPCServer
import os
import inspect

server = SimpleXMLRPCServer((’localhost’, 9000), logRequests=True)

def expose(f):
    "Decorator to set exposed flag on a function."
    f.exposed = True
    return f

def is_exposed(f):
    "Test whether another function should be publicly exposed."
    return getattr(f, ’exposed’, False)

class MyService: 
    PREFIX = ’prefix’
    def _dispatch(self, method, params):
        # Remove our prefix from the method name
        if not method.startswith(self.PREFIX + ’.’):
            raise Exception(’method "%s" is not supported’ % method)

        method_name = method.partition(’.’)[2]
        func = getattr(self, method_name)
        if not is_exposed(func):
            raise Exception(’method "%s" is not supported’ % method)

        return func(*params)
    
    @expose
    def public(self):
        return ’This is public’
    
    def private(self):
        return ’This is private’

server.register_instance(MyService())
try:
    print ’Use Control-C to exit’
    server.serve_forever()
except KeyboardInterrupt:
    print ’Exiting’


~~~~~~~~~~~~~~~~~~~~~~ client.py ~~~~~~~~~~~~~~~~~~~~~~
import xmlrpclib
proxy = xmlrpclib.ServerProxy(’http://localhost:9000’) 
print ’public():’, proxy.prefix.public()
try:
    print ’private():’, proxy.prefix.private() 
except Exception, err:
    print ’ERROR:’, err 
try:
    print ’public() without prefix:’, proxy.public() 
except Exception, err:
    print ’ERROR:’, err

Introspection API

有时候我们需要查询服务器有哪些可以调用的接口,这时候这个API就很有用了。
默认这些功能都是被关闭了。你可以通过 register_introspection_functions() 来打开。

from SimpleXMLRPCServer import SimpleXMLRPCServer, list_public_methods 
import os
import inspect

server = SimpleXMLRPCServer((’localhost’, 9000), logRequests=True)
server.register_introspection_functions()

class DirectoryService:
    def _listMethods(self):
        return list_public_methods(self)

    def _methodHelp(self, method):
        f = getattr(self, method) 
        return inspect.getdoc(f)

    def list(self, dir_name):
        """list(dir_name) => [<filenames>]

        Returns a list containing the contents of the named directory. 
        """
        return os.listdir(dir_name)

server.register_instance(MyService())
try:
    print ’Use Control-C to exit’
    server.serve_forever()
except KeyboardInterrupt:
    print ’Exiting’


~~~~~~~~~~~~~~~~~~~~~~ client.py ~~~~~~~~~~~~~~~~~~~~~~
import xmlrpclib
proxy = xmlrpclib.ServerProxy(’http://localhost:9000’) 
for method_name in proxy.system.listMethods():
    print ’=’ * 60
    print method_name
    print ’-’ * 60
    print proxy.system.methodHelp(method_name)
    print

多线程

这个SimpleXMLRPCServer是单任务的,即阻塞式。整成非阻塞式也非常方便:

from SimpleXMLRPCServer import SimpleXMLRPCServer
import SocketServer
class AsyncXMLRPCServer(SocketServer.ThreadingMixIn, SimpleXMLRPCServer):
    pass

server = AsyncXMLRPCServer(SERVER_URL, allow_none=True)

编码的坑

unicode中ASCII码低于空格的(比如这个黑桃字符♠️),在进行XML中传输时,客户端会抛出解析失败的错误:
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 9004, column 46
所以,要么需要对字符串进行一个safe check(将所有ASCII码低于空格的转成空格),要么转用其他协议传输(比如json)。

RPC的设计原则

一个良好的RPC系统设计主要由以下几个独立的模块组成:

  1. 数据结构 (requests/response/errors)
  2. 序列化 (JSON, XML, UI)
  3. 传输 (Socket, TCP/IP, HTTP)
  4. 代理/分发器dispatcher (映射函数调用到RPC)

开源选择

Reference:
https://www.simple-is-better.org/rpc/

    原文作者:blueshadow
    原文地址: https://www.jianshu.com/p/75526458e936
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞