Skip to main content

XML-RPC Server Simplification.

Project description

xmlrpcutils

XML-RPC Server Simplification.

安装

pip install xmlrpcutils

TestServer测试服务器

from xmlrpcutils.server import SimpleXmlRpcServer
from xmlrpcutils.service import ServiceBase

class SayHelloService(ServiceBase):

    def hello(self, name):
        return f"Hello {name}, how are you!"

class TestServer(SimpleXmlRpcServer):
    
    def register_services(self):
        super().register_services()
        SayHelloService(namespace="debug").register_to(self.server)

app =  TestServer()
app_ctrl = app.get_controller()

if __name__ == "__main__":
    app_ctrl()

启动测试服务器

python test_server.py

远程调用xmlrpc测试服务器

In [9]: from xmlrpc.client import ServerProxy

In [10]: server = ServerProxy('http://127.0.0.1:8381')

In [11]: server.system.listMethods()
Out[11]:
['debug.counter',
 'debug.echo',
 'debug.false',
 'debug.hello',
 'debug.hostname',
 'debug.null',
 'debug.ping',
 'debug.sleep',
 'debug.sum',
 'debug.timestamp',
 'debug.true',
 'debug.uname',
 'debug.urandom',
 'debug.uuid4',
 'system.listMethods',
 'system.methodHelp',
 'system.methodSignature',
 'system.multicall']

In [12]: server.debug.hello('world')
Out[12]: 'Hello world, how are you!'

使用apikey认证

服务器端在配置文件中增加apikeys配置

apikey-auth-header: apikey
apikeys:
  HyuTMsNzcSZYmwlVDdacERde9azdTKT8:
    appid: test01
    other-app-info: xxx
  SEEpVkus5b86aHxS6UMSCFLxkIhYMMZF:
    appid: test02
    other-app-info: xxx

客户端在初始化ServerProxy中指定

In [93]: from xmlrpc.client import ServerProxy
    ...: service = ServerProxy("http://127.0.0.1:8911", headers=[("apikey", "HyuTMsNzcSZYmwlVDdacERde9azdTKT8")])
    ...: result = service.debug.ping()
    ...: print(result)
pong

服务端启用keepalive

keepalive:
    enable: true
    timeout: 5
    max: 60

默认情况下,服务端不启用http的keepalive特性。在配置文件中设置keepalive.enable=true后启用。

启用服务端版本

server-tokens: true

默认情况下,服务端响应时已经将Server响应头隐藏。在配置文件中设置server-tokens=true显示。

启用methodSignature

def myfunc1(arg1:int, arg2:str, arg3:list) -> str:
    pass
  • 使用参数类型注释来启用methodSignature。
def myfunc2(arg1, arg2, arg3):
    """
    @methodSignature: ["str", "int", "str", "list"]
    """
    pass
  • 在函数文档中使用@methodSignature:来启用methodSignature。
def myfunc3(args, arg1, arg2, arg3):
    pass
myfunc3._methodSignature = ["str", "int", "str", "list"]
  • 使用函数属性来启用methodSignature。

其它配置项

pidfile: app.pid
daemon: True
workspace: /app
loglevel: INFO
logfile: app.log
logfmt: default
server:
  listen: ["0.0.0.0", 8381]
  daemon: false

注意

  • python3.7及以下版本,不支持在ServerProxy中使用headers参数,所以添加apikey检验机制时,需要使用高版本客户端。或定制transport参数。

版本

v0.1.1

  • 初始版本。

v0.1.2

  • 修正安装包打包时缺少license_files的问题。

v0.1.3

  • 修正DebugService的__init__方法,补充super().init()的调用。

v0.2.0

  • 不强制使用gevent。
  • 允许注册无命名空间函数。

v0.3.1

  • 清理gevent遗留。
  • 增加apikey认证机制。客户端通过headers指定apikey。

v0.3.2

  • 修正get_ignore_methods函数命名。

v0.4.0

  • 增加server-tokens选项,允许用户隐藏和显示Server请求头。默认为隐藏Server请求头。
  • 增加keepalive选项,支持keepalive特性。

v0.4.2

  • 文档更新。

v0.4.3

  • 文档修正。
  • 添加methodSignature支持。

v0.4.4

  • 修正methodSignature返回值类型。应该反馈[[...], [...]]类型,或"signatures not supported"。

v0.4.5

  • 文档更新。

v0.5.0

  • 添加xrpc命令。
  • 允许在配置在文件中指定services。

v0.5.1

  • Doc update.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xmlrpcutils-0.5.1.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xmlrpcutils-0.5.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file xmlrpcutils-0.5.1.tar.gz.

File metadata

  • Download URL: xmlrpcutils-0.5.1.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for xmlrpcutils-0.5.1.tar.gz
Algorithm Hash digest
SHA256 76070b4a59c8087a2f645678fde9b0e251886422d3ee6cf5f6b1e2440d6b72c1
MD5 3c9f40305639d9cfd6bb82dc29e172a6
BLAKE2b-256 cf89dd606ca83e4dbf89d0b8109b07818bf5ef0c35714b0c56d9c654d8e30a0d

See more details on using hashes here.

File details

Details for the file xmlrpcutils-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: xmlrpcutils-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for xmlrpcutils-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 59d0c2520416de51ed0cb1c2e312f6e281d2a441d8adc87c36811f1cf9b761be
MD5 3fa52668e350603823d81d9ed8b8d451
BLAKE2b-256 bb50a96d26fa35c4072b7c95c2e17bbe90a6b0743f530202c7a2833720be85a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page