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?