-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient2.py
More file actions
65 lines (50 loc) · 1.55 KB
/
client2.py
File metadata and controls
65 lines (50 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import websocket
from threading import Thread
import avro.schema
import io
import avro.io
from datetime import datetime
import json
output_file = 'Data{}.txt'.format(datetime.now().date())
test_schema = '''
{
"namespace": "example.avro",
"type": "record",
"name": "User",
"fields": [
{"name": "name", "type": "string"},
{"name": "favorite_number", "type": ["int", "null"]},
{"name": "favorite_color", "type": ["string", "null"]}
]
}
'''
schema = avro.schema.Parse(test_schema)
def on_message(ws, message):
print(message)
def run(*args):
raw_bytes = args[0]
bytes_reader = io.BytesIO(raw_bytes)
decoder = avro.io.BinaryDecoder(bytes_reader)
reader = avro.io.DatumReader(schema)
decoded_data = reader.read(decoder)
with open(output_file, 'a') as text_file:
text_file.write('\n Received at {:%Y-%m-%d %H:%M} :\n'.format(datetime.now()))
text_file.write(json.dumps(decoded_data))
t = Thread(target=run, args=(message,))
t.start()
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
print("### Initiating new websocket connection ###")
def initiate():
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://localhost:5670/",
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open
ws.run_forever()
if __name__ == "__main__":
initiate()