나는 서버 전송 이벤트 (; 텍스트/이벤트 스트림 콘텐츠 형식 SSE)에 대한 gzip 압축 을 가능하게 할 수 있는지 알고 싶습니다. http://chimera.labs.oreilly.com/books/1230000000545/ch16.htmlSSE (Server-Sent Events)에서 gzip 압축을 사용할 수 있습니까?
하지만 gzip 압축과 SSE의 예를 찾을 수 없습니다 :
이 책에 따르면,이 가능 보인다. 나는 응답 헤더 필드 콘텐츠 인코딩 세트 성공없이 "gzip을"에와 gzip으로 압축 된 메시지를 보낼 로했습니다. SSE 주위 실험에 대한
, 나는 병 프레임 워크 + gevent 파이썬에서 만든 작은 웹 응용 프로그램 을 테스트입니다; 난 그냥 병 WSGI 서버를 실행하고 있습니다 : 압축없이@bottle.get('/data_stream')
def stream_data():
bottle.response.content_type = "text/event-stream"
bottle.response.add_header("Connection", "keep-alive")
bottle.response.add_header("Cache-Control", "no-cache")
bottle.response.add_header("Content-Encoding", "gzip")
while True:
# new_data is a gevent AsyncResult object,
# .get() just returns a data string when new
# data is available
data = new_data.get()
yield zlib.compress("data: %s\n\n" % data)
#yield "data: %s\n\n" % data
코드를 (마지막 줄 주석)와 gzip없이 컨텐츠 인코딩 헤더 필드 마법처럼 작동합니다.
편집 : 응답에이 다른 질문에 대한 감사 : Python: Creating a streaming gzip'd file-like?, 내가 문제를 해결하기 위해 관리 :
@bottle.route("/stream")
def stream_data():
compressed_stream = zlib.compressobj()
bottle.response.content_type = "text/event-stream"
bottle.response.add_header("Connection", "keep-alive")
bottle.response.add_header("Cache-Control", "no-cache, must-revalidate")
bottle.response.add_header("Content-Encoding", "deflate")
bottle.response.add_header("Transfer-Encoding", "chunked")
while True:
data = new_data.get()
yield compressed_stream.compress("data: %s\n\n" % data)
yield compressed_stream.flush(zlib.Z_SYNC_FLUSH)
감사합니다. 사실, 압축을 풀기 위해 Content-Encoding을 변경하면 약간의 도움이됩니다. 첫 번째 메시지는 클라이언트 측에서 처리됩니다. 그러나 첫 번째 :(이유는 무엇입니까?) 미리 감사드립니다. – mguijarr
각 데이터 블록에 대해 독립적으로 compress를 호출하려고합니까? 작동하지 않을 것입니다. 모든 데이터는 단일 압축 스트림에 있어야합니다.즉, 스트리밍 인터페이스가있는 gzip이 실제로 이동하는 방법 일 수 있습니다. 그러나 특정 포인터를 제공하기 위해 더 많은 코드를 볼 필요가 있습니다. – otus
감사합니다. 마침내 그것은 작동한다! 나는 내 질문을 편집하여 내가 무엇을 바꾸어야하는지 알렸다. – mguijarr