简体   繁体   English

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

[英]How to get the response headers from a suds request

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

With more effort than ought be necessary is the answer. 答案是比应有的努力更多的努力。

I've got suds version 0.3.9 here. 我这里有泡沫版0.3.9。 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. 我必须使用传输类的子类,并将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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM