How to parse output from sse.client in Python?

I'm new to Python and trying to figure out how to parse SSE client code. I am using the SSE client library . My code is very simple and exactly matches the sample. There he is:

from sseclient import SSEClient


devID = "xxx"
AToken = "xxx"

sparkURL = 'https://api.spark.io/v1/devices/' + devID + '/events/?access_token=' + AToken

messages = SSEClient(sparkURL)

for msg in messages:
    print(msg)
    print(type(msg))

The code works without problems, and I see some empty lines and SSE data. Here is an example output:

<class 'sseclient.Event'>
{"data":"0 days, 0:54:43","ttl":"60","published_at":"2015-04-09T22:43:52.084Z","coreid":"xxxx"}
<class 'sseclient.Event'>

<class 'sseclient.Event'>
{"data":"0 days, 0:55:3","ttl":"60","published_at":"2015-04-09T22:44:12.092Z","coreid":"xxx"}
<class 'sseclient.Event'>

The actual output above looks like a dictionary, but its type is "sseclient.Event". I am trying to figure out how to parse the output so that I can pull out one of the fields, and nothing that I tried did not work.

Sorry if these are the main questions, but can anyone give some simple guidelines on how I would either convert all the output to a dictionary or maybe just pull one of the fields out?

Thank you in advance!

+4
1

. , - , . msg.data, msg. , JSON, .

messages = SSEClient(sparkURL)

for msg in messages:
    outputMsg = msg.data
    if type(outputMsg) is not str:
        outputJS = json.loads(outputMsg)
        FilterName = "data"
        #print( FilterName, outputJS[FilterName] )
        print(outputJS[FilterName])
+5

All Articles