Response codes and \r\n

Peter Otten __peter__ at web.de
Thu Mar 5 05:54:04 EST 2009


Catherine Heathcote wrote:

> I am reading an XML file (code at the end if it helps) and all goes well
> except I am getting the http response code printed. So everything (hat
> works of course) has "200 OK" on the first line. Am I missing some
> simple  way of surprising this, or should I just delete the 1st line
> before playing with the content?

Just remove the line

print(response.status, response.reason)

Whoever wrote that script *wanted* it to print that "200 OK"

> Also everything I get has "\r\n" in it, which atm I am getting rid of
> with strip(), is that the best way?

That is because you are getting back bytes, not a string. You can convert
the data with 

data = data.decode("utf-8") # or whatever the actual encoding is

and then mess around with string methods or regular expressions, but if you
want to do it right you have to learn about element tree

http://docs.python.org/3.0/library/xml.etree.elementtree.html#module-xml.etree.ElementTree

or one of the alternatives.

Also note that conn.close will *not* call the close method. It should be

conn.close()

Peter



More information about the Python-list mailing list