我不知道我是否要在这里发布它,但我正在尝试使用 python 和 suds 库请求 Mouser Cart API
def updateCart():
url = "https://mews.mouser.com/cartservice.asmx?op=UpdateCart&wsdl"
client = Client(url)
xmlns = Attribute("xmlns", "http://tempuri.org/XMLSchema.xsd")
xmlnsXSD = Attribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema")
xmlnsXSI = Attribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
cartGUID = Attribute("CartGUID", "")
requestor = Attribute("Requestor", "richeve")
cartMessage = Element("CartMessage") \
.append(xmlns) \
.append(xmlnsXSD) \
.append(xmlnsXSI) \
.append(cartGUID)\
.append(requestor)
partNumber = Attribute("MouserPartNumber", "941-CCS050M12CM2")
quantity = Attribute("Quantity", "5")
cartItem = Element("CartItem").append(partNumber).append(quantity)
cartMessage.append(cartItem)
xmlCartMessage = Element("xmlCartMessage").append(cartMessage)
result = client.service.UpdateCart(xmlCartMessage)
print result
print client
return True
问题是我的操作总是超时。我不知道他们的 API 或服务器是否出现故障。或者我的代码中遗漏了一些东西。
请您参考如下方法:
我刚刚参加了 python <-> Mouser 购物车 API 之战,今天赢了。这是我学到的。
超时是由 WSDL 底部的不正确端点引起的。它指定端口 9001,但那里没有任何监听。覆盖 suds 客户端位置以删除端口规范使其工作。
url = 'https://mews.mouser.com/cartservice.asmx?WSDL' location = 'https://mews.mouser.com/cartservice.asmx' client = Client(url, location=location, cache=None)client.service.UpdateCart()需要一个 string XML 文档。这对我有用:xmlCartMessage = Document() xmlCartMessage.append(cartMessage) result = client.service.UpdateCart(xmlCartMessage.plain())Mouser 的响应也是文本 —
suds.sax.text.TextXML 片段。参见 https://lists.fedoraproject.org/pipermail/suds/2011-October/001537.html有关此行为的描述。我用了https://github.com/martinblech/xmltodict将其整理成字典。import xmltodict d = xmltodict.parse(result)
