【问题标题】:How to get the response headers from a suds request如何从 suds 请求中获取响应标头
【发布时间】:2012-04-03 15:52:58
【问题描述】:

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

【问题讨论】:

    标签: python http soap suds


    【解决方案1】:

    答案是付出超出应有的努力。

    我这里有 suds 版本 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
    

    【讨论】:

    • 这看起来不错,非常感谢!我很高兴这并不明显。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 2020-11-16
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 2022-01-03
    • 2016-08-07
    相关资源
    最近更新 更多