如何告诉 Django 不要缓冲 HTTP POST 数据?
客户端将 mjpeg 流作为 HTTP POST 发布:
POST /feed/testfeed HTTP/1.0
Content-type: multipart/x-mixed-replace; boundary=--myboundary
--myboundary
Content-length: 14179
Content-type: image/jpeg
....JFIF....
....
我在 Django 中根本看不到传入数据。 request.read(6)
返回空字符串。我添加了假的“content-Length”标头:
POST /feed/testfeed HTTP/1.0
Content-Length: -1
Content-type: multipart/x-mixed-replace; boundary=--myboundary
...
现在它以最大速度读取整个数据。 request.read(6)
仅在我中断连接后才返回(包含整个数据,而不仅仅是预期的 6 个字节)。
当我使用“PUT”请求而不是“POST”请求时,也会出现同样的行为。
如何关闭 POST 请求的缓冲?
The client is posting mjpeg stream as HTTP POST:
POST /feed/testfeed HTTP/1.0
Content-type: multipart/x-mixed-replace; boundary=--myboundary
--myboundary
Content-length: 14179
Content-type: image/jpeg
....JFIF....
....
I see no incoming data in Django at all. request.read(6)
returns empty string. I add fake "content-Length" header:
POST /feed/testfeed HTTP/1.0
Content-Length: -1
Content-type: multipart/x-mixed-replace; boundary=--myboundary
...
Now it reads the whole data with maximum speed. request.read(6)
returns (with the whole data, not just expected 6 bytes) only after I interrupt the connection.
The same behaviour is when I use "PUT" request instead of "POST" one.
How to turn off buffering of the POST request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一点猜测(因为你没有解释你如何准确地为网站提供服务),但我认为不是 Django 进行缓冲,而是在它前面的 Web 服务器。是否可以禁用或缓解此问题取决于您实际使用的服务器。
您可能对以下内容感兴趣(对于 Nginx):
基本上看来,Nginx 禁用这种行为可能是不可能的(参见禁用 nginx 中的请求缓冲)。不确定其他服务器,但那里有很多讨论,所以使用谷歌应该会产生大量信息。
It's a little bit of a guess here (because you didn't explain how you are serving the website exactly), but I would think that it's not Django that does the buffering but a web server in front of it. Whether this can be disabled or mitigated depends on the server you're actually using.
You may be interested with the following (in case of Nginx):
Basically it seems, that with Nginx disabling this behavior might not be possible (see disable request buffering in nginx). Not sure about other servers, but there's plenty of discussion out there, so using google should yield lots of information.