Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Scala读取连续http流_Python_Scala_Bottle_Http Streaming_Akka Stream - Fatal编程技术网

Python Scala读取连续http流

Python Scala读取连续http流,python,scala,bottle,http-streaming,akka-stream,Python,Scala,Bottle,Http Streaming,Akka Stream,如何连接和读取scala中的连续(分块)http流?例如,如果我用python/Battle编写了这个简单的服务: from gevent import monkey; monkey.patch_all() import gevent from bottle import route, run @route('/stream') def stream(): while True: yield 'blah\n' gevent.sleep(1) run(

如何连接和读取scala中的连续(分块)http流?例如,如果我用python/Battle编写了这个简单的服务:

from gevent import monkey; monkey.patch_all()

import gevent
from bottle import route, run

@route('/stream')
def stream():
    while True:
        yield 'blah\n'
        gevent.sleep(1)

run(host='0.0.0.0', port=8100, server='gevent')

我计划使用akka stream来处理数据,我只需要一种方法来检索它。

这应该行得通。基本上,您只需对uri执行一个请求,即可生成分块响应。响应实体包含一个数据字节流。对于分块响应,这将是分块流。对于非分块响应(HttpEntity.Strict),这将是一个只有一个分块的流

显然,您也可以显式匹配实体,以查看它是否为HttpEntity.Chunked,但通常您希望保留处理非分块响应的能力

在真实的应用程序中,您不会使用runForeach来执行副作用,而是对数据字节流进行一些处理

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{Uri, HttpRequest}
import akka.stream.ActorMaterializer

object ChunkTestClient extends App {

  implicit val system = ActorSystem("test")
  import system.dispatcher

  implicit val materializer = ActorMaterializer()
  val source = Uri("https://jigsaw.w3.org/HTTP/ChunkedScript")
  val finished = Http().singleRequest(HttpRequest(uri = source)).flatMap { response =>
    response.entity.dataBytes.runForeach { chunk =>
      println(chunk.utf8String)
    }
  }
}