I'm trying to read (multiple) streams at the same time and extract only the JSON data from it.
So I have an ANPR camera that allows me to continiously "listen" to it and will send data on an URL with an image and json payload in it with the detected plate.
HTTP/1.1 200 OK
Pragma: no-cache
Expires: Thu, 01 Dec 2003 16:00:00 GMT
Connection: close
Content-Type: multipart/mixed; boundary=IPCamEventStreamBoundary
Cache: no-cache
Accept-Ranges: none
X-KeepAlive: 0
X-Timestamp: 1620986981098
X-Windows-Timestamp: 13265460581098
--IPCamEventStreamBoundary
Content-Type: image/jpeg
Content-Length: 498749
X-Timestamp: 1620986982002
X-Image-Index: 1
X-Frame-Id: 521699
X-Frame-Timestamp: 757030579
X-Frame-Width: 2560
X-Frame-Height: 1920
**binary data of the image goes here**
--IPCamEventStreamBoundary
Content-Type: application/json
Content-Length: 308
X-Event-Index: 1
X-Timestamp: 1620986982042
{
"DetectorVersion" : 131072,
"DetectorID" : "{6309907F-5708-47D1-B410-50F02C8882FB}",
"DetectorClassID" : -835316578,
"EventTime" : "13265460582042",
"State" : "dsSignal",
"EventCode" : 100,
"EventInfo" : {},
"EventID" : "{4F34A399-9E02-1846-ADA7-98A2798B46B9}",
"DetectorEventType" : "detSignal"
}
I only need the application/json
part.
What I currently have, is the whole readout of everything, but I can't seem to just "decode" the json and use that data.
This is my current code:
import json
import requests
r = requests.get('http://ip:port/stream', stream=True)
if r.encoding is None:
r.encoding = 'utf-8'
for line in r.iter_lines(decode_unicode=True):
if line:
try:
# print(json.loads(line))
print(line)
except:
pass
How am I supposed to do this? Just skipping the first binary content and only read the JSON?