-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
38 lines (32 loc) · 1016 Bytes
/
server.py
File metadata and controls
38 lines (32 loc) · 1016 Bytes
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
import io
import avro.schema
import avro.io
import asyncio
import random
import websockets
async def send_avro(websocket, path):
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)
writer = avro.io.DatumWriter(schema)
bytes_writer = io.BytesIO()
encoder = avro.io.BinaryEncoder(bytes_writer)
writer.write({"name": "Alyssa", "favorite_number": 256}, encoder)
raw_bytes = bytes_writer.getvalue()
while True:
print(raw_bytes)
await websocket.send(raw_bytes)
await asyncio.sleep(random.random() * 3)
start_server = websockets.serve(send_avro, 'localhost', 5670)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()