2014-01-13 20 views
2

我不知道如果我要在这里发布,但我想使用Python来请求给Mouser的车API和皂液库Mouser的车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或服务器是否发生故障。或者我在我的代码中缺少一些东西。

回答

2

我刚刚与python < - > Mouser推出的API战斗并赢得今天的胜利。这是我学到的东西。

  1. 超时是由WSDL底部的错误端点引起的。它指定了9001端口,但没有听到那里。覆盖泡沫客户端位置以删除端口规范使其工作。

    url = 'https://mews.mouser.com/cartservice.asmx?WSDL' 
    location = 'https://mews.mouser.com/cartservice.asmx' 
    client = Client(url, location=location, cache=None) 
    
  2. client.service.UpdateCart()想要一个 XML文档。这是对我工作:

    xmlCartMessage = Document() 
    xmlCartMessage.append(cartMessage) 
    result = client.service.UpdateCart(xmlCartMessage.plain()) 
    
  3. 在Mouser的反应也是文本的一个suds.sax.text.Text XML片段。有关此行为的说明,请参见https://lists.fedoraproject.org/pipermail/suds/2011-October/001537.html。我使用https://github.com/martinblech/xmltodict将其转换为字典。

    import xmltodict 
    d = xmltodict.parse(result)