python无法访问suds方法

时间:2012-07-28 19:40:05

标签: python linux api soap suds

我正在尝试使用python中的SUDS访问SOAP api

from suds.client import Client
def initialize():
    url = 'http://uuuuuuuuuuuuuuu.com/wewewe/WsNBI?wsdl'
    username = 'xxxxx'
    password = 'ppppppp'
    client = Client(url)
    print client
    result = client.service.Login(nbiLogin NBILogin(username,password),)
    print result

我无法调用Login方法,任何想法我怎么能这样做?

这些是查询返回的方法......

Suds ( https://fedorahosted.org/suds/ )  version: 0.4 GA  build: R699-20100913

Service ( WsNBIService ) tns="www.test.com"
   Prefixes (1)
      ns0 = "www.test.com"
   Ports (1):
      (WsNBIPort)
         Methods (5):
            GetClientAssociationInfo(nbiSession NBISession, clientAssociationReqData ClientAssociationReqData, )
            GetEvent(nbiSession NBISession, eventReqData EventReqData, )
            GetZDClientAssociationInfo(nbiSession NBISession, clientAssociationReqData ClientAssociationReqData, )
            Login(nbiLogin NBILogin, )
            Logout(nbiSession NBISession, )
         Types (22):
            GetClientAssociationInfo
            GetClientAssociationInfoResponse
            GetEvent
            GetEventResponse
            GetZDClientAssociationInfo
            GetZDClientAssociationInfoResponse
            Login
            LoginResponse
            Logout
            LogoutResponse
            authenticateResult
            clientAssociationDetail
            clientAssociationReqData
            clientAssociationResult
            eventDetail
            eventReqData
            eventResult
            eventType
            nbiLogin
            nbiResult
            nbiSession
            requestType

更新

#!/usr/bin/env python

from suds.client import Client

def initialize():
    url = 'http://xxxxxxx/xxxx/WsNBI?wsdl'
    username = 'xxxxx'
    password = 'pppppp'
    client = Client(url)
    login = client.factory.create("ns0:NBILogin")
    print login
    ws = login.nbiLogin(userName=username, password = password)
    result = client.service.Login(ws)
    print result
def main():
    initialize()

if __name__ == "__main__":
    main()


[root@server scripts]# ./flex_soap.py
(nbiLogin){
   UserName = None
   Password = None
 }
Traceback (most recent call last):
  File "./flex_soap.py", line 19, in ?
    main()
  File "./flex_soap.py", line 16, in main
    flexMaster()
  File "./flex_soap.py", line 12, in flexMaster
    ws = login.nbiLogin(userName=username, password = password)
AttributeError: nbiLogin instance has no attribute 'nbiLogin'

更新

#!/usr/bin/env python

from suds.client import Client

def initialize():
    url = 'http://xxxxx/intune/WsNBI?wsdl'
    username = 'uuuuu'
    password = 'pppp'
    client = Client(url)
    print client
    login = client.factory.create("ns0:NBILogin")
    print login
    login.UserName = username
    login.Password = password
    result = client.service.Login(login)
    print result
    event = client.factory.create("ns0:EventReqData")
    print event
def main():
    initialize()

if __name__ == "__main__":
    main()

[root@server scripts]# ./flex_soap.py

(nbiLogin){
   UserName = None
   Password = None
 }
(authenticateResult){
   Success = True
   Session =
      (nbiSession){
         Id = "0eda1622-473c-4dd6-b68e-4ff3c1ee27f6"
      }
 }
(eventReqData){
   EventType =
      (eventType){
         value = None
      }
   SerialNumbers =
      (SerialNumbers){
         SerialNumber[] = <empty>
      }
 }

任何想法我怎么能得到这个方法

GetEvent(nbiSession NBISession, eventReqData EventReqData, )

1 个答案:

答案 0 :(得分:1)

您的代码不是有效的Python。 Login(nbiLogin NBILogin, )表示方法Login接受NBILogin类型的单个参数。它不是您应该使用的文字语法。尝试这样的事情:

 login = client.factory.create("ns0:NBILogin")
 login.UserName = username 
 login.Password = password 
 result = client.service.Login(login)

此输出:

(authenticateResult){
   Success = True
   Session =
      (nbiSession){
         Id = "0eda1622-473c-4dd6-b68e-4ff3c1ee27f6"
      }
 }

表示result.Success == True and result.Session.Id == "0eda1622-473c-4dd6-b68e-4ff3c1ee27f6"

GetEvent(nbiSession NBISession, eventReqData EventReqData, )表示您需要2个NBISessionEventReqData类型的参数。

您可以从result获得的会话。构建EventReqData

(eventReqData){
   EventType =
      (eventType){
         value = None
      }
   SerialNumbers =
      (SerialNumbers){
         SerialNumber[] = <empty>
      }
 }

您需要创建EventTypeSerialNumbers

event_req_data = client.factory.create("ns0:EventReqData")
event_req_data.EventType = "put some appropriate event type here"
event_req_data.SerialNumbers = [10, 51, 1] # some serial numbers 

以上假设序列号是整数,否则通过SerialNumber以与所有其他对象相同的方式创建每个client.factory.create()

sns = event_req_data.SerialNumbers = client.factory.create('ns0:SerialNumbers')
for item in [10, 51, 1]:
    ns = client.factory.create('ns0:SerialNumber')
    ns.value = item
    sns.SerialNumber.append(ns)

我在列表中看不到SerialNumbersSerialNumber类型,因此可能会失败。

如果suds没有自己从字符串转换为EventType,那么您可以使用EventType明确地创建client.factory.create()

event_type = client.factory.create("ns0:EventType")
event_type.value = "put some appropriate event type here"
event_req_data.EventType = event_type

拨打电话:

event = client.service.GetEvent(login.Session, event_req_data)
相关问题