xmlrpc, extract data from http headers

Filip Wasilewski filipwasilewski at gmail.com
Sat Sep 16 08:18:40 EDT 2006


Milos Prudek wrote:
> > Overload the _parse_response method of Transport in your
> > BasicAuthTransport and extract headers from raw response. See the
> > source of xmlrpclib.py in the standard library for details.
>
> Thank you.
>
> I am a bit of a false beginner in Python. I have written only short scripts. I
> want to read "Dive into Python" to improve my knowledge. Your advice is
> perfect. It is my fault that I need a little more guidance.
>
> I am not sure how the headers will be passed from Transport to the instance of
> ServerProxy. That is, if I change the _parse_response method, how do I
> retreive the headers using the ServerProxy command?

Erm, now I see that my previous response was incorrect, sorry. The
headers are not passed to the _parse_response method.

A better solution would be to extract cookies from headers in the
request method and return them with response (see the code below). I
still wonder if there is an easy way to use CookieJar from cookielib
with xmlrpclib.

class CookieTransport(xmlrpclib.Transport):
    def request(self, host, handler, request_body, verbose=0):
        h = self.make_connection(host)
        if verbose:
            h.set_debuglevel(1)

        self.send_request(h, handler, request_body)
        self.send_host(h, host)
        self.send_user_agent(h)
        self.send_content(h, request_body)

        errcode, errmsg, headers = h.getreply()

        if errcode != 200:
            raise ProtocolError(
                host + handler,
                errcode, errmsg,
                headers
                )

        self.verbose = verbose

        cookies = self.get_cookies(headers)

        try:
            sock = h._conn.sock
        except AttributeError:
            sock = None

        response = self._parse_response(h.getfile(), sock)
        if len(response) == 1:
            response = response[0]

        return response, cookies

    def _get_cookies(self, headers):
        import Cookie
        c = []
        for v in headers.getheaders('set-cookie'):
            c.append(Cookie.SimpleCookie(v))
        return c

server = xmlrpclib.ServerProxy("http://localhost:8000",
transport=CookieTransport())
result, cookies = server.something.call()


best,
fw




More information about the Python-list mailing list