Praful Bagai
Praful Bagai

Reputation: 17402

Get status code for request sent using pysimplesoap

I'm using Pysimplesoap for sending a data to the web-service. I want to know the status of the request code. I'm able to print a traceback using trace=True. Over there it does prints the status code and other response variables but how do I get the to store all the traceback into a variable and then check the status code for the same?

Here's my code :_

client = SoapClient(
                    location = url,
                    action = 'http://tempuri.org/IService_1_0/',
                    namespace = "http://tempuri.org/", 
                    soap_ns='soap', ns = False,trace = True
                    )

data = {'AWB_Number' : '406438762211111', 'Weight':'0.023' ,'Length':'16.4','Height':'4.5','Width':'9.9'}
response= client.UpdateShipment(
                                ShipmentNumber = data['AWB_Number'], 
                                Weight = Decimal(data['Weight']), 
                                Length = Decimal(data['Length']), 
                                Height = Decimal(data['Height']), 
                                Width =  Decimal(data['Width']) , 
                                InboundLane = "2",
                                SequenceNumber = "1", 
                                )

I do get a traceback :-

Content-length: 526
Content-type: text/xml; charset="UTF-8"

<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header/>
<soap:Body>
    <UpdateShipment xmlns="http://tempuri.org/">
    <SequenceNumber>1</SequenceNumber><Weight>0.023</Weight><Height>4.5</Height><Width>9.9</Width><Length>16.4</Length><ShipmentNumber>406438762211111</ShipmentNumber><InboundLane>2</InboundLane></UpdateShipment>
</soap:Body>
</soap:Envelope>
status: 200
content-length: 293
x-powered-by: ASP.NET
server: Microsoft-IIS/7.5
date: Sat, 23 Aug 2014 07:27:38 GMT
content-type: text/xml; charset=utf-8
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><UpdateShipmentResponse xmlns="http://tempuri.org/"><UpdateShipmentResult/></UpdateShipmentResponse></s:Body></s:Envelope>
===============================================================================

There's a status code mention there as 200, but I dont get to store this traceback into a variable to get to know the status code of it. A human need to intervene to have a look at the status code. How do my program gets to know the status code?

Upvotes: 0

Views: 2257

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1124548

The SoapClient instance retains a .response attribute, containing information about the response. What that information is depends on the transport picked.

If you have just PySimpleSoap installed, the urllib2 library is picked and the status code is not part of the client.response attribute; the information is not retained from the actual response from urllib2, only the HTTP headers are preserved.

The pycurl transport gives you even less info; client.response is always an empty dictionary then.

Only if you also installed httplib2 will you get anything useful out of this; client.response is then set to a dictionary that includes a status code:

>>> import pysimplesoap
>>> client = pysimplesoap.client.SoapClient(wsdl='http://www.webservicex.net/stockquote.asmx?WSDL')
>>> response = client.GetQuote('GOOG')
>>> client.response
{'status': '200', 'content-length': '991', 'x-aspnet-version': '4.0.30319', 'vary': 'Accept-Encoding', 'server': 'Microsoft-IIS/7.0', '-content-encoding': 'gzip', 'cache-control': 'private, max-age=0', 'date': 'Sat, 23 Aug 2014 08:05:19 GMT', 'x-powered-by': 'ASP.NET', 'content-type': 'text/xml; charset=utf-8'}
>>> client.response['status']
'200'

Note that the value is a string, not an integer.

The httplib2 transport is picked by default if available.

As for the trace option; that sets up a logging module log, calls logging.basicConfig() with a log level and gets on with it. You could add a custom handler to the pysimplesoap.client.log object, but I wouldn't bother, really. If you see the status logged to the trace, then in all likelihood you are already using httplib2 and can access the status code more directly. For urllib2, for example, no status is logged either. That's because it is the client.response.items() values that are being logged here.

Upvotes: 1

Anshul Goyal
Anshul Goyal

Reputation: 77053

From the pysimplesoap source code, at line 261 of client, the send method is written which is used to send the http request from client's end

Taking cue from it,

def send(self, method, xml):
    ...    
    response, content = self.http.request(
        location, 'POST', body=xml, headers=headers)
    self.response = response
    self.content = content
    ...    

you should be able to access client.response to get the raw response for the request. From there, try doing a client.response.status, or do a dir(client.response) to find out any supportive method to get status code.

EDIT

As mentioned here in transport module, you can specifiy a library (like urllib2 or httplib2). to make it the default one to be picked up.

Upvotes: 1

Related Questions