How to get the response headers from a suds request
Asked Answered
R

1

5

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

Regelation answered 3/4, 2012 at 15:52 Comment(0)
S
7

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
Shuping answered 3/4, 2012 at 20:55 Comment(1)
This looks good, thanks very much! I'm glad it wasn't obvious.Regelation

© 2022 - 2024 — McMap. All rights reserved.