如何使用 Python 调用 Soap API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15436749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Call Soap API with Python
提问by user1330225
While I've used APIs in the past, this is the first SOAP I've attempted to use. I copy, pasted, and changed around some of this code from a SOAP tutorial, but I've seen it done 10 different ways in 10 different examples, yet none are very clear in explaining the code. Maybe the following code isn't the best way to do it, but that's why I'm looking for some help and a clear direction to go in. Thanks so much.
虽然我过去使用过 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
回答by user4815162342
Don't try to roll your own SOAP client — despite the name, SOAP is anything but simple.
不要尝试推出自己的 SOAP 客户端——尽管名称如此,但 SOAP 绝不简单。
Find any decent SOAP libraryand use it for your SOAP communication.
找到任何合适的 SOAP 库并将其用于您的 SOAP 通信。
Generally, the question of which SOAP library is "the best"is by nature contentious and the answer tends to vary with time, as projects come into and go out of fashion. Pick the one that works well for youruse case, and any one is likely to be better than writing your own.
一般来说,哪个 SOAP 库是“最好的”这个问题本质上是有争议的,随着项目的流行和过时,答案往往会随着时间而变化。选择适合您的用例的一种,并且任何一种都可能比自己编写更好。

