13.12. xmlrpc.server — xml-金年会app官方网
目的:实现 xml-rpc 服务器
xmlrpc.server
模块包含用于使用 xml-rpc 协议创建跨平台,独立于语言的服务器的类。客户端库除了 python 外还存在许多其他语言,这使得 xml-rpc 成为构建 rpc 样式服务的简便选择。
note
此处提供的所有示例都包括一个客户端模块以及与演示服务器进行交互的功能。要运行示例,请使用两个单独的外壳窗口,一个用于服务器,一个用于客户端。
一个简单的服务器
这个简单的服务器示例公开了一个使用目录名称并返回内容的函数。第一步是创建simplexmlrpcserver
实例并告诉它侦听传入请求的位置(在这种情况下为 localhost 端口 9000)。然后,将一个功能定义为服务的一部分,并进行注册,以便服务器知道如何调用它。最后一步是将服务器置于无限循环中,以接收和响应请求。
warning
此实现具有明显的安全隐患。不要在开放式 internet 上的服务器上或可能存在安全性问题的任何环境中运行它。
xmlrpc_function.py
from xmlrpc.server import simplexmlrpcserver
import logging
import os
# set up logging
logging.basicconfig(level=logging.info)
server = simplexmlrpcserver(
('localhost', 9000),
logrequests=true,
)
# expose a function
def list_contents(dir_name):
logging.info('list_contents(%s)', dir_name)
return os.listdir(dir_name)
server.register_function(list_contents)
# start the server
try:
print('use control-c to exit')
server.serve_forever()
except keyboardinterrupt:
print('exiting')
可以使用通过url http:/// localhost:9000
访问服务器。 html#module-xmlrpc.client“ xmlrpc.client:xml-rpc的客户端库”)。此客户端代码说明了如何从 python 调用list_contents()
服务。
xmlrpc_function_client.py
import xmlrpc.client
proxy = xmlrpc.client.serverproxy('http://localhost:9000')
print(proxy.list_contents('/tmp'))
serverproxy
使用其基本url连接到服务器,然后直接在代理上调用方法。在代理上调用的每个方法都转换为对服务器的请求。使用 xml 格式化参数,然后通过 post 消息将其发送到服务器。服务器解压缩 xml,并根据从客户端调用的方法名称确定要调用的函数。将参数传递给函数,然后将返回值转换回 xml,以返回给客户端。
启动服务器将给出以下输出。
$ python3 xmlrpc_function.py
use control-c to exit
在第二个窗口中运行客户端将显示/ tmp
目录的内容。
$ python3 xmlrpc_function_client.py
['com.apple.launchd.aogxonn8nv', 'com.apple.launchd.ilryiaqugf',
'example.db.db',
'ksoutofprocessfetcher.501.ppfihqx0vjatsb8ajyobdv7cu68=',
'pymotw_import_example.shelve.db']
请求完成后,日志输出将显示在服务器窗口中。
$ python3 xmlrpc_function.py
use control-c to exit
info:root:list_contents(/tmp)
127.0.0.1 - - [18/jun/2016 19:54:54] "post /rpc2 http/1.1" 200 -
输出的第一行来自list_contents()
中的logging.info()
调用。第二行来自服务器记录请求的服务器,因为logrequests
是true
。
备用api名称
有时,模块或库中使用的函数名称不是外部 api 中应使用的名称。名称可能会更改,因为加载了特定于平台的实现,基于配置文件动态构建了服务 api ,或者可以用存根替换实际功能以进行测试。要使用其他名称注册函数,请将名称作为第二个参数传递给register_function()
。
xmlrpc_alternate_name.py
from xmlrpc.server import simplexmlrpcserver
import os
server = simplexmlrpcserver(('localhost', 9000))
def list_contents(dir_name):
"expose a function with an alternate 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')
客户端现在应该使用名称dir()
代替list_contents()
。
xmlrpc_alternate_name_client.py
import xmlrpc.client
proxy = xmlrpc.client.serverproxy('http://localhost:9000')
print('dir():', proxy.dir('/tmp'))
try:
print('.list_contents():', proxy.list_contents('/tmp'))
except xmlrpc.client.fault as err:
print('.error:', err)
调用list_contents()
会导致错误,因为服务器不再具有使用该名称注册的处理程序。
$ python3 xmlrpc_alternate_name_client.py
dir(): ['com.apple.launchd.aogxonn8nv',
'com.apple.launchd.ilryiaqugf', 'example.db.db',
'ksoutofprocessfetcher.501.ppfihqx0vjatsb8ajyobdv7cu68=',
'pymotw_import_example.shelve.db']
error: :method "list_contents"
is not supported'>
点缀的api名称
可以使用 python 标识符通常不合法的名称来注册各个函数。例如,名称中可以包含句点(。
),以分隔服务中的命名空间。下一个示例扩展了“目录”服务,以添加“创建”和“删除”调用。使用前缀“ dir。
”注册所有功能,以便同一台服务器可以使用不同的前缀提供其他服务。此示例中的另一个区别是某些函数返回none
,因此必须告知服务器将none
值转换为nil值。
xmlrpc_dotted_name.py
from xmlrpc.server 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')
要在客户端中调用服务功能,只需用虚线名称引用它们即可。
xmlrpc_dotted_name_client.py
import xmlrpc.client
proxy = xmlrpc.client.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'))
假设当前系统上没有/tmp/example
文件,则示例客户端脚本的输出如下。
$ python3 xmlrpc_dotted_name_client.py
before : false
create : none
should exist : true
remove : none
after : false
任意api名称
另一个有趣的功能是能够用其他方式无效的 python 对象属性名称注册函数。此示例服务注册了一个名称为“ multiply args
”的函数。
xmlrpc_arbitrary_name.py
from xmlrpc.server 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')
由于注册名称包含空格,因此不能使用点符号直接从代理访问它。但是,使用getattr()
是可行的。
xmlrpc_arbitrary_name_client.py
import xmlrpc.client
proxy = xmlrpc.client.serverproxy('http://localhost:9000')
print(getattr(proxy, 'multiply args')(5, 5))
但是,避免使用这样的名称创建服务。提供此示例不一定是因为它是一个好主意,而是因为存在具有任意名称的现有服务,并且新程序可能需要能够调用它们。
$ python3 xmlrpc_arbitrary_name_client.py
25
公开对象的方法
前面的部分讨论了使用良好的命名约定和命名空间来建立 api 的技术。将命名空间纳入 api 的另一种方法是使用类的实例并公开其方法。可以使用一个方法的实例重新创建第一个示例。
xmlrpc_instance.py
from xmlrpc.server 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')
客户端可以直接调用该方法。
xmlrpc_instance_client.py
import xmlrpc.client
proxy = xmlrpc.client.serverproxy('http://localhost:9000')
print(proxy.list('/tmp'))
输出显示目录的内容。
$ python3 xmlrpc_instance_client.py
['com.apple.launchd.aogxonn8nv', 'com.apple.launchd.ilryiaqugf',
'example.db.db',
'ksoutofprocessfetcher.501.ppfihqx0vjatsb8ajyobdv7cu68=',
'pymotw_import_example.shelve.db']
但是,该服务的“ dir。
”前缀已丢失。可以通过定义一个类来建立可以从客户端调用的服务树来恢复它。
xmlrpc_instance_dotted_names.py
from xmlrpc.server 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(root, allow_dotted_names=true)
try:
print('use control-c to exit')
server.serve_forever()
except keyboardinterrupt:
print('exiting')
通过在启用allow_dotted_names
的情况下注册serviceroot
的实例,服务器有权在请求进入时使用getattr()
查找命名方法,从而遍历对象树.
xmlrpc_instance_dotted_names_client.py
import xmlrpc.client
proxy = xmlrpc.client.serverproxy('http://localhost:9000')
print(proxy.dir.list('/tmp'))
dir.list()
的输出与以前的实现相同。
$ python3 xmlrpc_instance_dotted_names_client.py
['com.apple.launchd.aogxonn8nv', 'com.apple.launchd.ilryiaqugf',
'example.db.db',
'ksoutofprocessfetcher.501.ppfihqx0vjatsb8ajyobdv7cu68=',
'pymotw_import_example.shelve.db']
分发调用
默认情况下,register_instance()
查找名称不是以下划线开头的实例的所有可调用属性(“ _
”)并使用其名称注册它们。要对公开的方法更加小心,可以使用自定义调度逻辑。
xmlrpc_instance_with_prefix.py
from xmlrpc.server 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 "{}" is not supported'.format(method)
)
method_name = method.partition('.')[2]
func = getattr(self, method_name)
if not is_exposed(func):
raise exception(
'method "{}" is not supported'.format(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')
myservice
的public()
方法被标记为暴露给xml-rpc服务,而private()
则没有。当客户端尝试访问myservice
的一部分的函数时,将调用_dispatch()
方法。它首先强制使用前缀(在这种情况下为“ prefix。
”,但可以使用任何字符串)。然后它要求该函数具有一个名为exposed
的属性,该属性具有真实值。为了方便,使用装饰器在函数上设置了暴露标志。下面的示例包括一些示例客户端调用。
xmlrpc_instance_with_prefix_client.py
import xmlrpc.client
proxy = xmlrpc.client.serverproxy('http://localhost:9000')
print('public():', proxy.prefix.public())
try:
print('private():', proxy.prefix.private())
except exception as err:
print('.error:', err)
try:
print('public() without prefix:', proxy.public())
except exception as err:
print('.error:', err)
结果输出以及预期的错误消息被捕获并报告。
$ python3 xmlrpc_instance_with_prefix_client.py
public(): this is public
error: :method "prefix.private" is
not supported'>
error: :method "public" is not
supported'>
还有其他几种方法可以覆盖调度机制,包括直接从simplexmlrpcserver
进行子类化。有关更多详细信息,请参考模块中的文档字符串。
自省api
与许多网络服务一样,可以查询 xml-rpc 服务器以询问其支持什么方法并学习如何使用它们。 simplexmlrpcserver
包括一组用于执行此自省的公共方法。默认情况下,它们是关闭的,但可以通过register_introspection_functions()
启用。可以通过定义_listmethods()
和_methodhelp()
。
xmlrpc_introspection.py
from xmlrpc.server 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) => []
returns a list containing the contents of
the named directory.
"""
return os.listdir(dir_name)
server.register_instance(directoryservice())
try:
print('use control-c to exit')
server.serve_forever()
except keyboardinterrupt:
print('exiting')
在这种情况下,便利功能list_public_methods()
扫描实例以返回不以下划线开头的可调用属性的名称(_
)。重新定义_listmethods()
以应用所需的任何规则。类似地,对于此基本示例,_methodhelp()
返回函数的文档字符串,但可以编写该文档字符串以从另一个来源构建帮助字符串。
该客户端查询服务器并报告所有可公开调用的方法。
xmlrpc_introspection_client.py
import xmlrpc.client
proxy = xmlrpc.client.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()
系统方法包括在结果中。
$ python3 xmlrpc_introspection_client.py
============================================================
list
------------------------------------------------------------
list(dir_name) => []
returns a list containing the contents of
the named directory.
============================================================
system.listmethods
------------------------------------------------------------
system.listmethods() => ['add', 'subtract', 'multiple']
returns a list of the methods supported by the server.
============================================================
system.methodhelp
------------------------------------------------------------
system.methodhelp('add') => "adds two integers together"
returns a string containing documentation for the specified method.
============================================================
system.methodsignature
------------------------------------------------------------
system.methodsignature('add') => [double, int, int]
returns a list describing the signature of the method. in the
above example, the add method takes two integers as arguments
and returns a double result.
this server does not support system.methodsignature.
另请参见
-
-[xmlrpc.client
](“ xmlrpc.client:xml-rpc的客户端库”) – xml-rpc客户端。
- –介绍如何使用xml-rpc来以多种语言实现客户端和服务器。
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 cc 协议,如果我们的工作有侵犯到您的权益,请及时联系金年会app官方网。