如何在Python中使用Suds提取方法

时间:2014-01-03 12:04:42

标签: python-2.7 suds

我想提取所有方法,并希望使用如何使用python进行自动化来发送一些参数。

我只希望方法作为用户输入并将参数发送到方法。 我怎样才能做到这一点?

from suds.client import client
url="name fo the url"
client=Client(url)
Suds ( https://fedorahosted.org/suds/ )  version: 0.4 GA  build: R699-20100913

Service ( Services ) tns="http://www.altoromutual.com/bank/ws/"
Prefixes (1)
ns0 = "http://www.altoromutual.com/bank/ws/"
Ports (2):
(ServicesSoap)
 Methods (3):
    GetUserAccounts(xs:int UserId, )
    IsValidUser(xs:string UserId, )
    TransferBalance(MoneyTransfer transDetails, )
 Types (4):
    AccountData
    ArrayOfAccountData
    MoneyTransfer
    Transaction
  (ServicesSoap12)
 Methods (3):
    GetUserAccounts(xs:int UserId, )
    IsValidUser(xs:string UserId, )
    TransferBalance(MoneyTransfer transDetails, )
 Types (4):
     AccountData
    ArrayOfAccountData
    MoneyTransfer
    Transaction 

1 个答案:

答案 0 :(得分:18)

列出WSDL中可用的所有方法:

>>> from suds.client import Client
>>> url_service = 'http://www.webservicex.net/globalweather.asmx?WSDL'

>>> client = Client(url_service)
>>> list_of_methods = [method for method in client.wsdl.services[0].ports[0].methods]

>>> print list_of_methods
[GetWeather, GetCitiesByCountry]

然后调用方法本身:

>>> response = client.service.GetCitiesByCountry(CountryName="France")

注意:“Python Web Service Client Using SUDS and ServiceNow”提供了一些简单示例。

关注@ kflaw的评论,这是如何检索应该传递给方法的参数列表:

>>> method = client.wsdl.services[0].ports[0].methods["GetCitiesByCountry"]
>>> params = method.binding.input.param_defs(method)
>>> print params
[(CountryName, <Element:0x10a574490 name="CountryName" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />)