HTTPResponse.read() returns an empty string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christoph Söllner

    HTTPResponse.read() returns an empty string?

    Hi again,

    my Source:
    """
    import httplib
    conn = httplib.HTTPCon nection('www.py thon.org');
    conn.request("G ET", "/index.html");
    answ = conn.getrespons e();
    print answ.status, answ.reason[color=blue][color=green][color=darkred]
    >>> 200 OK[/color][/color][/color]
    conn.close();
    print "Start";[color=blue][color=green][color=darkred]
    >>> Start[/color][/color][/color]
    print answ.read();[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    print len(answ.read() );[color=blue][color=green][color=darkred]
    >>> 0[/color][/color][/color]
    print "End";[color=blue][color=green][color=darkred]
    >>> End[/color][/color][/color]

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

    Thanks again,
    Chris


  • Christoph Söllner

    #2
    Re: HTTPResponse.re ad() returns an empty string?

    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.clos e()
    command; very strange behaviour.[color=blue]
    > """
    > import httplib
    > conn = httplib.HTTPCon nection('www.py thon.org');
    > conn.request("G ET", "/index.html");
    > answ = conn.getrespons e();
    > print answ.status, answ.reason[color=green][color=darkred]
    >>>> 200 OK[/color][/color]
    > conn.close();
    > print "Start";[color=green][color=darkred]
    >>>> Start[/color][/color]
    > print answ.read();[color=green][color=darkred]
    >>>>[/color][/color]
    > print len(answ.read() );[color=green][color=darkred]
    >>>> 0[/color][/color][/color]


    Comment

    • Marc 'BlackJack' Rintsch

      #3
      Re: HTTPResponse.re ad() returns an empty string?

      In <dj2m7k$s7n$1@w sc10.lrz-muenchen.de>, Christoph Söllner wrote:
      [color=blue]
      > ok got it:
      > One cannot close the connection before reading the answer.[/color]

      Yep, because the "answer" is read over the connection.
      [color=blue]
      > Seems that in my original source the new assigned variable
      > 'answ' is destroyed or emptied with the connection.clos e()
      > command; very strange behaviour.[/color]

      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

      Comment

      Working...