【问题标题】:How to Call Soap API with Python如何使用 Python 调用 Soap API
【发布时间】:2023-04-08 03:38:01
【问题描述】:

虽然我过去使用过 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

【问题讨论】:

    标签: python api soap


    【解决方案1】:

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

    更新: Base suds 项目很长时间未激活。当前项目有一个新的分支,现在非常活跃。

    asuds project

    【讨论】:

    • 网址失效了,能否提供一个新的网址?
    【解决方案2】:

    不要尝试推出自己的 SOAP 客户端 - 尽管名称如此,SOAP 绝不简单。

    找到任何decent SOAP library 并将其用于您的 SOAP 通信。

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

    【讨论】:

    • 按照 Python 文档规则,没有理由告诉用户 做什么。相反,您应该解释 OP 代码中的问题,或者只是建议这可能是一个解决方案,而不是阻止另一个解决方案。
    • @Yuval 答案确实提出了一个更好的解决方案 - 请参阅第二段。出于充分的理由,出于善意,并在回答明确询问方向的问题时,提出了反对此处未发明的方法的建议。两年后,即使您不同意语气,这仍然是个好建议。
    • @user4815162342 请更新您的答案。链接现在不可用。
    猜你喜欢
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    • 2021-10-04
    • 2021-05-08
    • 1970-01-01
    相关资源
    最近更新 更多