gpt4 book ai didi

python - 可迭代对象和 Django StreamingHttpResponse

转载 作者:太空狗 更新时间:2023-10-30 00:04:59 33 4
gpt4 key购买 nike

我想用 django 连接到内部 http 服务,我需要缓冲这些服务的 http 响应的输出,因为有些内容非常大。

我正在使用 python 3.6、django 2.0、http.client 和以下代码:

class HTTPStreamIterAndClose():
def __init__(self, conn, res, buffsize):
self.conn = conn
self.res = res
self.buffsize = buffsize
self.length = 1

bytes_length = int(res.getheader('Content-Length'))

if buffsize < bytes_length:
self.length = math.ceil(bytes_length/buffsize)

def __iter__(self):
return self

def __next__(self):
buff = self.res.read(self.buffsize)

if buff is b'':
self.res.close()
self.conn.close()

raise StopIteration
else:

return buff

def __len__(self):
return self.length


def passthru_http_service(request, server, timeout, path):
serv = HTTPService(server, timeout)
res = serv.request(path)

response = StreamingHttpResponse(
HTTPStreamIterAndClose(serv.connection, res, 200),
content_type='application/json'
)
response['Content-Length'] = res.getheader('Content-Length')

return response

响应为空,我用以下方法测试迭代器:

b''.join(HTTPStreamIterAndClose(serv.connection, res, 200)

一切正常,我不知道为什么不工作。

最佳答案

https://andrewbrookins.com/django/how-does-djangos-streaminghttpresponse-work-exactly/

First, some conditions must be true:

  • The client must be speaking HTTP/1.1 or newer
  • The request method wasn’t a HEAD
  • The response does not include a Content-Length header
  • The response status wasn’t 204 or 304

If these conditions are true, then Gunicorn will add a Transfer-Encoding: chunked header to theresponse, signaling to the client that the response will stream inchunks.

In fact, Gunicorn will respond with Transfer-Encoding: chunked even ifyou used an HttpResponse, if those conditions are true!

To really stream a response, that is, to send it to the client inpieces, the conditions must be true, and your response needs to be aniterable with multiple items.

基本上,您需要决定:流式传输还是 Content-Length

如果您想要断点续传,请使用 Range .

关于python - 可迭代对象和 Django StreamingHttpResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48015016/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com