import httplib

__license__ = "MIT"
__author__ = "Christian Wyglendowski"

class IterableResponse(httplib.HTTPResponse, object):
    def __iter__(self):
        """Iterate over the chunks of an HTTP response with chunked transfer-coding.

        Adapted from httplib.HTTPResponse._read_chunked.
        """

        assert self.chunked != httplib._UNKNOWN, "Response is not chunked!"
        chunk_left = self.chunk_left

        while True:
            if chunk_left is None:
                line = self.fp.readline()
                i = line.find(';')
                if i >= 0:
                    line = line[:i] # strip chunk-extensions
                chunk_left = int(line, 16)
                if chunk_left == 0:
                    break
            yield self._safe_read(chunk_left)

            # we read the whole chunk, get another
            self._safe_read(2)      # toss the CRLF at the end of the chunk
            chunk_left = None

        # read and discard trailer up to the CRLF terminator
        ### note: we shouldn't have any trailers!
        while True:
            line = self.fp.readline()
            if not line:
                # a vanishingly small number of sites EOF without
                # sending the trailer
                break
            if line == '\r\n':
                break

        # we read everything; close the "file"
        self.close()

if __name__ == "__main__":
    conn = httplib.HTTPConnection('localhost:8080')
    conn.response_class = IterableResponse
    conn.request('GET', '/')
    r = conn.getresponse()
    for chunk in r:
        print chunk

