Milos Prudek wrote:
Overload the _parse_response method of Transport in your
BasicAuthTransp ort 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.Tran sport):
def request(self, host, handler, request_body, verbose=0):
h = self.make_conne ction(host)
if verbose:
h.set_debugleve l(1)
self.send_reque st(h, handler, request_body)
self.send_host( h, host)
self.send_user_ agent(h)
self.send_conte nt(h, request_body)
errcode, errmsg, headers = h.getreply()
if errcode != 200:
raise ProtocolError(
host + handler,
errcode, errmsg,
headers
)
self.verbose = verbose
cookies = self.get_cookie s(headers)
try:
sock = h._conn.sock
except AttributeError:
sock = None
response = self._parse_res ponse(h.getfile (), sock)
if len(response) == 1:
response = response[0]
return response, cookies
def _get_cookies(se lf, headers):
import Cookie
c = []
for v in headers.gethead ers('set-cookie'):
c.append(Cookie .SimpleCookie(v ))
return c
server = xmlrpclib.Serve rProxy("http://localhost:8000" ,
transport=Cooki eTransport())
result, cookies = server.somethin g.call()
best,
fw