Using http.client
in Python 3.3+ (or any other builtin python HTTP client library), how can I read a chunked HTTP response exactly one HTTP chunk at a time?
I'm extending an existing test fixture (written in python using http.client
) for a server which writes its response using HTTP's chunked transfer encoding. For the sake of simplicity, let's say that I'd like to be able to print a message whenever an HTTP chunk is received by the client.
My code follows a fairly standard pattern for reading a large response:
conn = http.client.HTTPConnection(...)
conn.request(...)
response = conn.getresponse()resbody = []while True:chunk = response.read(1024)if len(chunk):resbody.append(chunk)else:breakconn.close();
But this reads 1024 byte chunks regardless of whether or not the server is sending 10 byte chunks or 10MiB chunks.
What I'm looking for would be something like the following:
while True:chunk = response.readchunk()if len(chunk):resbody.append(chunk)elsebreak
If this is not possible with http.client
, is it possible with another builtin http client library? If it's not possible with a builtin client lib, is it possible with pip
installable module?