Programmatically invoke RPC methods for a SOAP endpoint in python

那年仲夏 提交于 2019-12-09 06:07:30

问题


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)

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)

(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.

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".

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.


回答1:


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)



来源:https://stackoverflow.com/questions/36891049/programmatically-invoke-rpc-methods-for-a-soap-endpoint-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!