Как получить заголовки ответа из запроса suds - PullRequest
5 голосов
/ 03 апреля 2012

Я использую модуль python suds и хотел бы получить заголовки ответа (в частности, Last-Modified) из ответа suds.

1 Ответ

7 голосов
/ 04 апреля 2012

С большим усилием, чем необходимо, - ответ.

У меня есть версия 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
...