gevent.http.HTTPServer提供的API似乎支持两个方向的流式传输.请求对象不提供请求体作为简单字符串,而是提供一个
Python迭代的.input_buffer属性,而在另一个方向上,响应的数据可以作为具有三个调用的块传递:
request.send_reply_start(200, 'OK')
request.send_reply_chunk(...) # as many times as you wish
request.send_reply_end()
但我必须有一些错误配置的东西,因为尽管这个奇妙的无缓冲的API,我的请求处理程序直到最后一块请求POST数据终于到达时才被调用,而在另一方向我没有看到任何标题到达我的客户端套接字直到服务器到达.send_reply_end().是否有一些我必须抛出的开关或一些配置设置我必须操作才能关闭缓冲并查看请求并在响应时发送响应,例如gevent通过其StreamServer支持原始套接字?
我的应用程序需要支持可能比RAM大的单文件上载和下载,这将需要关闭此缓冲.
这是一个简单的服务器和使用gevent编写的客户端,它应该显示这种行为:
# srv.py
import gevent.http
M100 = 100 * 1024 * 1024
def main():
print 'Serving on 8088...'
gevent.http.HTTPServer(('0.0.0.0', 8088), handle).serve_forever()
def handle(request):
print 'Is request chunked?', request.chunked
for item in request.input_buffer:
print 'received body segment of length', len(item), 'bytes'
request.add_output_header('Content-Type', 'application/octet-stream')
request.send_reply_start(200, 'OK')
for i in range(5):
print 'sending chunk', i
request.send_reply_chunk(M100 * 'x')
request.send_reply_end()
if __name__ == '__main__':
main()
和:
# cli.py
import requests
import time
M100 = 100 * 1024 * 1024
def gen():
for i in range(5):
print 'sending chunk', i
yield M100 * 'x'
time.sleep(1)
if __name__ == '__main__':
r = requests.post('http://localhost:8088/', data=gen(), stream=True)
for block in r.iter_content(M100):
print 'received', len(block), 'bytes from download'
谢谢你的指导!