multiple send on httplib.HTTPConnection, and multi

2019-07-14 07:29发布

问题:

Should it be possible to send a plain, single http POST request (not chunk-encoded), in more than one segment? I was thinking of using httplib.HTTPConnection and calling the send method more than once (and calling read on the response object after each send).

(Context: I'm collaborating to the design of a server that offers services analogous to transactions [series of interrelated requests-responses]. I'm looking for the simplest, most compatible HTTP representation.)

回答1:

After being convinced by friends that this should be possible, I found a way to do it. I override httplib.HTTPResponse (n.b. httplib.HTTPConnection is nice enough to let you specify the response_class it will instantiate).

Looking at socket.py and httplib.py (especially _fileobject.read()), I had noticed that read() only allowed 2 things:

  • read an exact number of bytes (this returns immediately, even if the connection is not closed)
  • read all bytes until the connection is closed

I was able to extend this behavior and allow free streaming with just a few lines of code. I also had to set the will_close member of my HTTPResponse to 0.

I'd still be interested to hear if this is considered acceptable or abusive usage of HTTP.



标签: python http