开发者

multiple send on httplib.HTTPConnection, and multiple read on HTTPResponse?

开发者 https://www.devze.com 2023-02-12 18:09 出处:网络
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

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开发者_如何学C 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.)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消