views:

92

answers:

3

I am working with urllib2, and trying to extract the headers in a printable form from a Response object.

Presently I am printing str(response.info()), however what is printed, is itself a Python string (at least to my understanding).

(Pdb) p str(response.info())
'Date: Tue, 23 Feb 2010 03:12:26 GMT\r\nServer: Apache\r\nVary: Accept-Encoding,User-Agent\r\nContent-Encoding: gzip\r\nContent-Length: 9045\r\nConnection: close\r\nContent-Type: text/html; charset=ISO-8859-1\r\n'

I need to turn that string into an "actual" string, such as by evaluation or something similar. The best theoretical solution I've found is to use:

s = str(response.info())
print s.decode("string_escape")

But this does not work. Further adding to the confusion is how to handle the quotes within the string, calling eval(s) and str(s) do not work either.

Is there some better way to extract the raw headers in the response without quoting, or a method to decode the string s as above?

A: 

response.info() returns a httplib.HTTPMessage, which behaves like a mapping:

info = response.info()
for k, v in info.items():
  print '%s: %s' % (k, v)

So in short, you're doing it wrong.

Ignacio Vazquez-Abrams
yes but i want to do it this way
Matt Joiner
+1  A: 

From pdb, this should work:

print str(response.info())

Not sure if that answers your question, though.

Marcelo Cantos
yeah, it returns a python escape string, and prints that quotes and all. nto what i'm after
Matt Joiner
It doesn't on my machine. `p` prints the Python-syntax string, whereas `print` yields multi-line output with no visible escapes.
Marcelo Cantos
p is a debugger command. print isn't a debugger command, so is executed as a python statement
gnibbler
+1: You were right but didn't connect debugger behaviour to my problem.
Matt Joiner
+3  A: 

str(info()) does give a normal string:

>>> import urllib2
>>> f = urllib2.urlopen('http://tejp.de')
>>> print str(f.info())
Connection: close
Vary: Accept-Encoding
Content-Type: text/html
Accept-Ranges: bytes
ETag: "-807357257"
Last-Modified: Wed, 01 Jul 2009 10:05:34 GMT
Content-Length: 285
Date: Tue, 23 Feb 2010 03:24:10 GMT
Server: lighttpd/1.4.19

It's only the debugger's p command which prints the string in escaped form.

sth
nice work, didn't even realise
Matt Joiner