python使用suds访问用.net开发的webservice的API接口


背景:

这个是中东一个快递公司的api接口,背景就是要调用该公司的api接口,查询快递的物流信息,由于它是使用webservice的方式访问,所以特此记录一下

笔者是使用python3.6

安装:

pip3.6 install suds-jurko


代码:

from suds.client import Client
url = Default.ARAMEX_URL
url = "http://ws.aramex.net/ShippingAPI.V2/Tracking/Service_1_0.svc?singleWsdl"
client = Client(url)

print(client)


输出内容:

Suds ( https://fedorahosted.org/suds/ )  version: 0.6

Service ( Service_1_0 ) tns="http://ws.aramex.net/ShippingAPI/v1/"
   Prefixes (3)
      ns0 = "http://schemas.microsoft.com/2003/10/Serialization/"
      ns1 = "http://schemas.microsoft.com/2003/10/Serialization/Arrays"
      ns2 = "http://ws.aramex.net/ShippingAPI/v1/"
   Ports (2):
      (BasicHttpBinding_Service_1_0)
         Methods (2):
            TrackPickup(ClientInfo ClientInfo, Transaction Transaction, xs:string Reference)
            TrackShipments(ClientInfo ClientInfo, Transaction Transaction, ns1:ArrayOfstring Shipments, xs:boolean GetLastTrackingUpdateOnly)
         Types (11):
            ns1:ArrayOfKeyValueOfstringArrayOfTrackingResultmFAkxlpY
            ArrayOfNotification
            ArrayOfTrackingResult
            ns1:ArrayOfstring
            ClientInfo
            Notification
            TrackingResult
            Transaction
            ns0:char
            ns0:duration
            ns0:guid
      (BasicHttpBinding_Service_1_01)
         Methods (2):
            TrackPickup(ClientInfo ClientInfo, Transaction Transaction, xs:string Reference)
            TrackShipments(ClientInfo ClientInfo, Transaction Transaction, ns1:ArrayOfstring Shipments, xs:boolean GetLastTrackingUpdateOnly)
         Types (11):
            ns1:ArrayOfKeyValueOfstringArrayOfTrackingResultmFAkxlpY
            ArrayOfNotification
            ArrayOfTrackingResult
            ns1:ArrayOfstring
            ClientInfo
            Notification
            TrackingResult
            Transaction
            ns0:char
            ns0:duration
            ns0:guid

解析一下:

我是选择了它默认的接口版本 BasicHttpBinding_Service_1_0(那你可以看出还有一个BasicHttpBinding_Service_1_01的版本)

你看到标红色字体的TrackPickup,TrackShipments就是该版本的接口,提供两个方法,一TrackPickup为追踪退回的运单,一个TrackShipments是查询订单

ClientInfo,Transaction,ns1:ArrayOfstring,xs:boolean蓝色字体为: 入存参数的类型,

ClientInfo,Transaction,Shipments,GetLastTrackingUpdateOnly 黑色字体(蓝色后的)为: 入存参数 



完整代码:

from suds.client import Client
url = Default.ARAMEX_URL
client = Client(url)

clientobj = client.factory.create('ClientInfo')
clientobj.UserName = Default.ARAMEX_USER_NAME
clientobj.Password = Default.ARAMEX_PASSWORD
clientobj.Version = Default.ARAMEX_VERSION
clientobj.AccountNumber = Default.ARAMEX_ACCOUNT_NUMBER
clientobj.AccountPin = Default.ARAMEX_ACCOUNT_PIN
clientobj.AccountEntity = Default.ARAMEX_ACCOUNT_ENTITY
clientobj.AccountCountryCode = Default.ARAMEX_ACCOUNT_COUNTRY_CODE

transactionobj = client.factory.create('Transaction')
transactionobj.Reference1 = ''
transactionobj.Reference2 = ''
transactionobj.Reference3 = ''
transactionobj.Reference4 = ''
transactionobj.Reference5 = ''

shipments = client.factory.create('ns1:ArrayOfstring')
shipments.string = [31043002045, 31043002266]

# True仅获取最后末条信息, False 获取所有末条信息
res = client.service.TrackShipments(clientobj, transactionobj, shipments, False)

要点:

1,创建参数可以根据client.factory.create('')创建某种类型的参数.

2,要是想知道该类型下需要填入什么信息(如,UserName,Password等)那么,你需要打开url,看里面需要传一些什么参数


备注:

希望能给第一次访问webservice  API的朋友们一些认识,也作为自己第一次使用此类型api的一个记录


你可能感兴趣的:(python)