在 python 中以编程方式调用 SOAP 端点的 RPC 方法 [英] Programmatically invoke RPC methods for a SOAP endpoint in python

查看:20
本文介绍了在 python 中以编程方式调用 SOAP 端点的 RPC 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种通过 Python 以编程方式调用 SOAP/RPC 调用的简单方法.类似的东西:

I'm looking for a simple way to programmatically invoke a SOAP/RPC call via Python. Something like:

method_to_invoke, args = parse_user_input()
outbound_xml = library.call_remote_method(method_to_invoke, args)
result = requests.post(... data=outbound_xml)

我知道有几个 Python 库支持 SOAP/RPC 调用;然而,他们都做了一些魔法",并允许这样的事情:

I know there are several Python libraries that support SOAP/RPC calls; however they all do some "magic" and allow for things like:

result = client.service.getPercentBodyFat('jeff', 68, 170)

(前面的示例取自 suds 文档,但基本原理相同).这假设我提前知道方法的名称并且无法在运行时确定它.

(previous example taken from suds documentation but the basic principles are the same). This assumes I know the name of the method ahead of time and can't determine it at runtime.

寻找不再维护的解决方案,或者尝试做太多魔术".例如,请参阅这个过于复杂的方案此解决方案 基本上是构建您自己的 XML 并将其发送".

Looking for solutions they are either no longer maintained, or try to do too much "magic". For example see this over-complicated soution or this solution which is basically "Build your own XML and send it over".

是否有任何库可以为我构建出站"XML 而无需通过箍?我已经有一个 HTTP 服务器,它将接收入站 RPC 并希望使用 requests 来正确处理诸如 SSL 证书验证之类的事情.

Is there any library that can build the "outbound" XML for me without having to go through hoops? I already have a HTTP server which would receive the inbound RPC and want to use requests to properly handle things like SSL certificate validation.

推荐答案

你可以给 python-zeep 一个机会 (http://docs.python-zeep.org).它可以通过以下代码轻松生成您要发送的xml:

You can give python-zeep a chance (http://docs.python-zeep.org). It can easily generate the xml which you want to send via the following code:

client = zeep.Client(
    wsdl='http://www.webservicex.net/ConvertSpeed.asmx?WSDL')
doc = client.service._binding.create_message('ConvertSpeed', 100, 
      'kilometersPerhour', 'milesPerhour'))    
print(etree.tostring(doc, pretty_print=True))

(我是作者,如果您对此有任何疑问,请告诉我)

(I'm the author so let me know if you have any issues with this)

这篇关于在 python 中以编程方式调用 SOAP 端点的 RPC 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆