如何从suds请求中获取响应头

时间:2012-04-03 15:52:58

标签: python http soap suds

我正在使用python suds模块,并希望从suds响应中检索响应头(特别是Last-Modified)。

1 个答案:

答案 0 :(得分:7)

应该付出更多的努力才能得到答案。

我这里有泡沫版0.3.9。我必须使用传输类的子类并包装send方法来存储传输类中最后收到的头文件。

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