Response codes and \r\n

  • Thread starter Catherine Heathcote
  • Start date
C

Catherine Heathcote

Heya,
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?

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

http://pastebin.com/m4120242

Thanks
 
P

Peter Otten

Catherine said:
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
 
M

Marc 'BlackJack' Rintsch

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?

Is this line actually part of `data`!? I would guess it is printed in
line 22, so it is not part of `data` and there is no need to delete it.
Also everything I get has "\r\n" in it, which atm I am getting rid of
with strip(), is that the best way?

At which point do you get rid of it and why?

BTW the last line of the code snippet needs parenthesis to actually
*call* the `conn.close` method.

Ciao,
Marc 'BlackJack' Rintsch
 
R

Richard Brodie

=
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.

I suggest you comment out line 22. The status shouldn't be in the data.
Also everything I get has "\r\n" in it, which atm I am getting rid of with strip(), is
that the best way?

I would use and XML parser such as Elementtree, and let it handle it.
Resist the temptation to think "it's a simple format, I'll parse it myself".
Otherwise strip() or rstrip('\r\n') is fine, depending how much whitespace
matters.
conn.close

Note that statement does nothing, it's not the same as conn.close()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top