Reputation: 61
I have a soap api and i want to call it with suds in python. I have install the suds and able to connect with the soap api.
client = Client("http://my_sevone_appliance/soap3/api.wsdl")
But catch is i have to send some headers and extra headers to the soap api and i also have to send the ssl file in TLSv1.
ssl.wrap_socket()
Remember python version is 2.7
but somehow i am not able to do that. can someone help me how to call the soap api from suds and pass headers in that and also ssl. thankyou
Upvotes: 1
Views: 6087
Reputation: 21
#**If you don't have any cert file and key:**
import ast
from suds.client import Client
headers='{}'
url = <wsdl url of soap api eg: url ="http://my_sevone_appliance/soap3/api.wsdl">
client = Client(url,location=url)
client.options.cache.clear()
c.set_options(headers=ast.literal_eval(headers))
c=client.service.<service name>(__inject={'msg': xml}) #eg. c.service.getName(__inject={'msg': xml})
#**fetching data from response**
#for example if the output will:
# c.CustomerInfo
#(ArrayOfCustomer){
# Customer[] =
# (Customer){
# Account = xyz
# AccountType = abc
# Account_TypeID = mno
# Address = None
# Address2 = None
# City = Pune
#},
#}
#then fetch like: c.CustomerInfo.Customer[0].Account
Upvotes: 2
Reputation: 61
I found the solution.I have done all these thing in python and suds. Please install suds.
So first i have write a tranport.py script in python which actually take the cert and key file and pass it to suds client. here is the code.
import urllib2, httplib, socket
from suds.transport.http import HttpTransport, Reply, TransportError
class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
def __init__(self, key, cert):
urllib2.HTTPSHandler.__init__(self)
self.key = key
self.cert = cert
def https_open(self, req):
#Rather than pass in a reference to a connection class, we pass in
# a reference to a function which, for all intents and purposes,
# will behave as a constructor
return self.do_open(self.getConnection, req)
def getConnection(self, host, timeout=300):
return httplib.HTTPSConnection(host,
key_file=self.key,
cert_file=self.cert)
class HTTPSClientCertTransport(HttpTransport):
def __init__(self, key, cert, *args, **kwargs):
HttpTransport.__init__(self, *args, **kwargs)
self.key = key
self.cert = cert
def u2open(self, u2request):
"""
Open a connection.
@param u2request: A urllib2 request.
@type u2request: urllib2.Requet.
@return: The opened file-like urllib2 object.
@rtype: fp
"""
tm = self.options.timeout
url = urllib2.build_opener(HTTPSClientAuthHandler(self.key, self.cert))
if self.u2ver() < 2.6:
socket.setdefaulttimeout(tm)
return url.open(u2request)
else:
return url.open(u2request, timeout=tm)
to use the above one and call soap api successfully i use following code.
url = <wsdl url of soap api eg: url ="http://my_sevone_appliance/soap3/api.wsdl">
pem = <is the key file>
cert = <is the cert>
c = Client(url, transport=HTTPSClientCertTransport(pem, cert))
then i call the function which i want to hit. you can see all fucntion you want to hit by.
print c
Here i have to use the headers also to send in the soap. So i have to pass the headers also(Optional).
import ast
headers = '{}'
c.set_options(headers=ast.literal_eval(headers))
i have to use ast because you have to send headers in json format. Then i call the function.
xml=<the xml data which you want to send>
example :
xml ='<soap:Env xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ser="http://www.abc1.com/services">
<soap:Header/>
<soap:Body>
<ser:getName>
<ser:ver>1</ser:ver>
<ser:syncId>222</ser:syncID>
<ser:mobileNo>0987654321</ser:mobileNo>
</ser:getBalance>
</soap:Body>
</soap:Env>'
now hit the soap function
c.service.getName(__inject={'msg': xml})
here getName is my function in soap api.
thats it. Ask me if you have any question.
Upvotes: 2