如何用Python调用SOAP API
虽然我之前用过API,但这是我第一次尝试使用SOAP。我从一个SOAP教程中复制、粘贴并修改了一些代码,但我看到的10个不同的例子都有不同的写法,然而没有一个能清楚地解释这些代码。也许下面的代码不是最好的写法,但这正是我寻求帮助和明确方向的原因。非常感谢。
import string, os, sys, httplib
server_addr = "auctions.godaddy.com"
service_action = "GdAuctionsBiddingWSAPI/GetAuctionList"
body = """
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.example.com/services/wsdl/2.0">
<soapenv:Header/>
<soapenv:Body>
<ns:serviceListRequest>
<ns:userInfo>
</ns:userInfo>
</ns:serviceListRequest>
</soapenv:Body>
</soapenv:Envelope>"""
request = httplib.HTTPConnection(server_addr)
request.putrequest("POST", service_action)
request.putheader("Accept", "application/soap+xml, application/dime, multipart/related, text/*")
request.putheader("Content-Type", "text/xml; charset=utf-8")
request.putheader("Cache-Control", "no-cache")
request.putheader("Pragma", "no-cache")
request.putheader("SOAPAction", "https://auctions.godaddy.com/gdAuctionsWSAPI/gdAuctionsBiddingWS.asmx?op=GetAuctionList" + server_addr + service_action)
request.putheader("Content-Length", "length")
request.putheader("apiKey", "xxxxxx")
request.putheader("pageNumber", "1")
request.putheader("rowsPerPage", "1")
request.putheader("beginsWithKeyword", "word")
request.endheaders()
request.send(body)
response = request.getresponse().read()
print response
2 个回答
10
别自己去做SOAP客户端——虽然名字里有“简单”,但SOAP可一点都不简单。
找一个靠谱的SOAP库来处理你的SOAP通信。
一般来说,关于哪个SOAP库是“最好”的这个问题,大家的看法往往不一样,而且随着时间的推移,答案也会变化,因为有些项目会流行,有些则会被淘汰。选择一个适合你自己需求的库,任何一个库都比自己写要好。