如何使用Python调用Soap API

时间:2013-03-15 15:54:11

标签: python api soap

虽然我过去使用过API,但这是我尝试使用的第一个SOAP。我从SOAP教程中复制,粘贴和更改了一些代码,但我已经看到它在10个不同的示例中以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 个答案:

答案 0 :(得分:10)

不要尝试使用自己的SOAP客户端 - 尽管名称如此,但SOAP并不简单。

查找任何decent SOAP library并将其用于SOAP通信。

一般来说,which SOAP library is "the best"的问题本质上是有争议的,随着时间的推移,答案往往会随着项目的进入而变得过时。选择适用于 用例的那个,任何一个都可能比编写自己的用例更好。

答案 1 :(得分:1)

我可以建议你使用suds。它非常好并且被广泛使用。

更新:基础泡沫项目长时间不活跃。当前项目有一个新的分支,现在非常活跃。

asuds project