suds调用webservice

一、安装

  pip install suds

二、日志  

  import logging

     logging.basicConfig(level=logging.INFO)

     logging.getLogger(“suds.client”).setLevel(logging.DEBUG)

三、基本方法,创建client

  3.1 通过wsdL的url创建一个client实例,获取服务 提供的方法(Methods ) 数量数据类型(types)及 数量

    from suds import Client

    url="http:///www.thomas-bayer.com/axis2/services/BLZService?wsdl”

    client = Client(url)

    print client

    返回如下:

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

        Service ( BLZService ) tns=”http://thomas-bayer.com/blz/”
           Prefixes (1)
              ns0 = “http://thomas-bayer.com/blz/”
           Ports (2):
              (BLZServiceSOAP11port_http)
                 Methods (1):
                    getBank(xs:string blz, )
                 Types (3):
                        detailsType
                        getBankResponseType
                    getBankType
              (BLZServiceSOAP12port_http)
                    Methods (1):
                        getBank(xs:string blz, )
                    Types (3):
                        detailsType
                        getBankResponseType
                        getBankType

 

  3.2 简单的的参数调用:

   3.2.1 单个服务器端口的直接调用

       result = client.service.getBack(‘ICBC’)

                  print result

   3.2.2 多个服务器端口的:

    1、不指定端口的同单个服务器端口直接调用,服务会通过默认的端口处理

    2、选择一个端口指定为默认端口

      client.set_options(prot ='BLZServiceSOAP11port_http')

    3、带服务端口名称的方法:

       result = client.service[‘BLZServiceSOAP11port_http’].getBank(‘ICBC’)

       print result

    4、带服务端口索引的方法:

      result= client。service.[0].getBank('ICBC')

      print result

   3.2.3 多服务与多端口:(类似与多端口的二维情况)

    1、不指定服务器和端口

    2、设置默认的服务和端口:

      client.set_potions(service=’serviceName’, port=’portName’)

    3、带服务名称和端口名称:

      client.service[‘serviceName’][‘portName’].method()

    4、带服务索引和端口索引:

      client.service[serviceIndex][portIndex].method()

  3.3复杂的参数调用:

    1、使用factory.create方法根据wsdl中的格式创建复杂对象(create complex objects )

      例如:http://apisandbox.4pxtech.com:8090/OrderOnline/ws/OrderOnlineService.dll?wsdl  参考文件:fourpx.rar

      中创建复杂对象:createOrderRequest

      order = client.factory.create("createOrderRequest”)

      然后向将order中写入需要的数据,必须的参数minOccurs="1",非必须参数:minOccurs="0"

      将order传给需要createOrderRequest类型参数的方法

      详细参考:https://fedorahosted.org/suds/wiki/Documentation#SERVICESWITHMULTIPLEPORTS 中的FACTORY方法说明

    2、HEADERS(参照HEADERS方法)      

client = client(url)
token = client.factory.create('AuthToken')
token.username = 'Elvis'
token.password = 'TheKing'
client.set_options(soapheaders=token) 

    3、COSTOM SOAP HEADERS(参照COSTOM SOAP HEADERS方法)

4、PLUGINS     

 

    

 

posted @ 2014-03-30 16:03  半天的半天  阅读(2061)  评论(0编辑  收藏  举报