如何用Python调用Soap API

2024-04-30 06:30:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: 方法代码comserverrequestserviceactionsoap
2条回答

不要试图滚动您自己的SOAP客户机-尽管有这个名称,SOAP可不简单。

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

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

我建议你用suds。它是相当好和广泛使用。

更新:基本suds项目已很长时间处于非活动状态。当前项目有一个新的分支,现在很活跃。

asuds project

相关问题 更多 >