By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,215 Members | 1,951 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,215 IT Pros & Developers. It's quick & easy.

xmlrpc, extract data from http headers

P: n/a
I perform a XML-RPC call by calling xmlrpclibBasicAuth which in turn calls
xmlrpclib. This call of course sends a HTTP request with correct HTTP
headers. The response is correctly parsed by xmlrpclib, and I get my desired
values.

However, I also need to get the raw HTTP headers from the HTTP response. There
is a cookie in the HTTP response and I need to read that cookie.

How could I do that?

--
Milos Prudek
Sep 15 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Milos Prudek wrote:
I perform a XML-RPC call by calling xmlrpclibBasicAuth which in turn calls
xmlrpclib. This call of course sends a HTTP request with correct HTTP
headers. The response is correctly parsed by xmlrpclib, and I get my desired
values.

However, I also need to get the raw HTTP headers from the HTTP response. There
is a cookie in the HTTP response and I need to read that cookie.

How could I do that?
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.

cheers,
fw

Sep 15 '06 #2

P: n/a
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?
--
Milos Prudek
Sep 16 '06 #3

P: n/a
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

Sep 16 '06 #4

P: n/a
A better solution would be to extract cookies from headers in the
request method and return them with response (see the code below). I
Full solution! Wow! Thank you very much. I certainly do not deserve such
kindness. Thanks a lot Filip!

--
Milos Prudek
Sep 19 '06 #5

P: n/a
Milos Prudek wrote:
A better solution would be to extract cookies from headers in the
request method and return them with response (see the code below). I

Full solution! Wow! Thank you very much. I certainly do not deserve such
kindness. Thanks a lot Filip!
Glad to help. All in all this is the purpose of this group, isn't it?

cheers,
fw

Sep 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.