HTTPResponse.read() returns an empty string?

C

Christoph Söllner

Hi again,

my Source:
"""
import httplib
conn = httplib.HTTPConnection('www.python.org');
conn.request("GET", "/index.html");
answ = conn.getresponse();
print answ.status, answ.reasonconn.close();
print "Start";
And the header states a content length of 11kBytes. What am I doin wrong?

Thanks again,
Chris
 
C

Christoph Söllner

ok got it:
One cannot close the connection before reading the answer.
Seems that in my original source the new assigned variable
'answ' is destroyed or emptied with the connection.close()
command; very strange behaviour.
 
M

Marc 'BlackJack' Rintsch

ok got it:
One cannot close the connection before reading the answer.

Yep, because the "answer" is read over the connection.
Seems that in my original source the new assigned variable
'answ' is destroyed or emptied with the connection.close()
command; very strange behaviour.

No, it's not emptied or destroyed. The read() method reads the content
over the connection. That doesn't work if the connection is closed. It's
like closing a file and then reading from it.

The response object contains just the headers. So you can inspect them
before you decide to download the actual content.

Ciao,
Marc 'BlackJack' Rintsch
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top