Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the response headers from a suds request

I'm using the python suds module and would like to retrieve the response headers (specifically Last-Modified) from a suds response.

like image 403
Pricey Avatar asked Apr 03 '12 15:04

Pricey


1 Answers

With more effort than ought be necessary is the answer.

I've got suds version 0.3.9 here. I had to subclass the transport class in use and wrap the send method to store the last received headers on in the transport class.

import logging
logging.basicConfig(level=logging.INFO)
#logging.getLogger('suds.client').setLevel(logging.DEBUG)
#logging.getLogger('suds.transport').setLevel(logging.DEBUG)
#logging.getLogger('suds.xsd.schema').setLevel(logging.DEBUG)
#logging.getLogger('suds.wsdl').setLevel(logging.DEBUG)

from suds.client import Client
from suds.xsd.doctor import ImportDoctor, Import
from suds.transport.https import HttpAuthenticated

class MyTransport(HttpAuthenticated):
    def __init__(self,*args,**kwargs):
        HttpAuthenticated.__init__(self, *args, **kwargs)
        self.last_headers = None

    def send(self,request):
        result = HttpAuthenticated.send(self, request)
        self.last_headers = result.headers
        return result

doctor = ImportDoctor(Import('http://schemas.xmlsoap.org/soap/encoding/'))
svc_url  = 'https://server/Service?wsdl'
svc_user = 'username'
svc_pass = 'password'

client = Client(svc_url,doctor=doctor,transport=MyTransport())
# For some reason I can't be bothered to investigate, setting the username and password in
# client kwargs doesn't pass them to the custom transport:
client.set_options(location=svc_url.partition('?')[0],username=svc_user,password=svc_pass)
# call a method
client.service.SomeMethod()
# look at headers
client.options.transport.last_headers
like image 128
MattH Avatar answered Oct 28 '22 22:10

MattH