How to Call Soap API with Python

api, python, soap

Solution

Don't try to roll your own SOAP client — despite the name, SOAP is anything but simple.

Find any decent SOAP library and use it for your SOAP communication.

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 your use case, and any one is likely to be better than writing your own.

Problem

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. ``` 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 ```

Original source

Related problems