解析扭曲的 dataReceived 中的数据

parse data in twisted dataReceived

我正在开发 Sonos 控制器 Kivy 应用程序(运行正在 RPi 上运行)。 sonos 方面正在使用 node.js。我的 kivy 应用程序当前发送一个 http 请求来获取 sonos 的状态(音量、电台、歌曲等),然后更新标签和图像。这很好用,但我想改用 twisted。作为起点,我 运行 在 kivy 文档 (https://kivy.org/docs/guide/other-frameworks.html) 中找到示例 Echo Server 应用程序。当我 运行 它时,dataReceived 正确获取有关 Sonos 状态更改的当前状态信息。这太棒了。不幸的是,数据是文本和 json 的混合体。我想知道是否有办法解析返回的 json。这是数据

content-type: application/json content-length: 1570 host: localhost:8000 connection: close

{ "type": "mute-change", "data": { "uuid": "RINCON_000000000000001400", "previousMute": true, "previousMute": false, "roomName": "Office" } }

除了使用dataReceived,还有更好的方法吗?我一直在寻找一种方法,只获取 json (body) 而没有所有 header 信息,但还没有找到有效的方法。

TIA

使用Twisted Web。例如:

from twisted.internet import reactor, endpoints
from twisted.web.server import Site
from twisted.web.resource import Resource
import time

class EchoPage(Resource):
    isLeaf = True
    def render_GET(self, request):
        return "I got: {}".format(
            request.content.read(),
        )

resource = EchoPage()
factory = Site(resource)
endpoint = endpoints.TCP4ServerEndpoint(reactor, 8880)
endpoint.listen(factory)
reactor.run()