2018-10-17 19:16:37 +02:00
|
|
|
import requests
|
|
|
|
import threading
|
|
|
|
import json
|
|
|
|
|
|
|
|
class EventClient:
|
2020-10-08 21:08:34 +02:00
|
|
|
def __init__(self,url, chunk_size = 1024):
|
|
|
|
self.chunk_size = chunk_size
|
2018-10-17 19:16:37 +02:00
|
|
|
self.r = requests.get(url,stream=True)
|
|
|
|
if self.r.encoding is None:
|
|
|
|
self.r.encoding = 'utf8'
|
|
|
|
|
2020-10-08 21:08:34 +02:00
|
|
|
def parseEvent(self, raw):
|
|
|
|
lines = raw.splitlines()
|
|
|
|
data = dict()
|
|
|
|
for line in lines:
|
|
|
|
k,v = line.split(":",1)
|
|
|
|
if data.get(k):
|
|
|
|
data[k] += v
|
|
|
|
data[k] = v
|
|
|
|
return data
|
|
|
|
|
2018-10-17 19:16:37 +02:00
|
|
|
def events(self):
|
2020-10-08 21:08:34 +02:00
|
|
|
def generate():
|
|
|
|
sbuf = ""
|
|
|
|
while True:
|
|
|
|
bbuf = self.r.raw._fp.fp.read1(self.chunk_size)
|
|
|
|
|
|
|
|
if not bbuf:
|
|
|
|
break
|
|
|
|
|
|
|
|
sbuf += bbuf.decode('utf8')
|
|
|
|
parts = sbuf.split('\n\n')
|
|
|
|
if len(parts) > 1:
|
|
|
|
for p in parts[0:-1]:
|
|
|
|
yield self.parseEvent(p)
|
|
|
|
|
|
|
|
sbuf = parts[-1]
|
|
|
|
return generate()
|
2018-10-17 19:16:37 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-10-08 21:08:34 +02:00
|
|
|
while True:
|
|
|
|
e = EventClient("http://localhost:5000/push")
|
|
|
|
for evt in e.events():
|
|
|
|
print(json.loads(evt['data']))
|
2018-10-17 19:16:37 +02:00
|
|
|
|