423,127 Members | 952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,127 IT Pros & Developers. It's quick & easy.

HTTPResponse.read() returns an empty string?

P: n/a
Hi again,

my Source:
"""
import httplib
conn = httplib.HTTPConnection('www.python.org');
conn.request("GET", "/index.html");
answ = conn.getresponse();
print answ.status, answ.reason
200 OK conn.close();
print "Start"; Start print answ.read(); print len(answ.read()); 0 print "End"; End


And the header states a content length of 11kBytes. What am I doin wrong?

Thanks again,
Chris
Oct 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.
"""
import httplib
conn = httplib.HTTPConnection('www.python.org');
conn.request("GET", "/index.html");
answ = conn.getresponse();
print answ.status, answ.reason
200 OK conn.close();
print "Start"; Start print answ.read(); print len(answ.read()); 0

Oct 18 '05 #2

P: n/a
In <dj**********@wsc10.lrz-muenchen.de>, Christoph Söllner wrote:
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
Oct 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.