问题描述
是否可以在多个段中发送纯单个http POST请求(不是块编码)?我在考虑使用 httplib.HTTPConnection
并多次调用发送
方法(并调用在每个
。发送
之后,在响应对象上读取
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
).
(上下文:I我正在合作设计一个服务器,提供类似于事务的服务[一系列相互关联的请求 - 响应]。我正在寻找最简单,最兼容的HTTP表示。)
(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.)
推荐答案
在被朋友说服这应该成为可能之后,我找到了一种方法。我重写 httplib.HTTPResponse
(nb httplib.HTTPConnection
足够让你指定 response_class
它将实例化。)
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).
查看socket.py和httplib.py(特别是 _fileobject.read()
),我注意到 read()
只允许2件事:
Looking at socket.py and httplib.py (especially _fileobject.read()
), I had noticed that read()
only allowed 2 things:
- 读取确切的字节数(即使连接未关闭,也会立即返回)
- 读取所有字节,直到连接关闭
我能够扩展这种行为并允许使用几行代码进行免费流式传输。我还必须将HTTPResponse的 will_close
成员设置为0.
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.
我仍然有兴趣听到如果这被认为是可接受的或滥用HTTP。
I'd still be interested to hear if this is considered acceptable or abusive usage of HTTP.
这篇关于多次发送httplib.HTTPConnection,多次读取HTTPResponse?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!