-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_client.py
74 lines (66 loc) · 2.47 KB
/
test_client.py
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
66
67
68
69
70
71
72
73
74
import asyncio
import websockets
import json
import requests
import random
import gzip
import base64
# Fetch the list of datasets from the server
async def fetch_datasets():
uri = "http://localhost:8080/datasets" # HTTP URL for listing datasets
response = requests.get(uri)
if response.status_code == 200:
return response.json()
else:
print("Failed to fetch datasets")
return []
# Fetch and stream data for a given dataset in batches
async def fetch_dataset(dataset_name):
uri = "ws://localhost:8080/ws" # WebSocket URL for streaming data
async with websockets.connect(uri) as websocket:
# Send request to start the data stream
request = {
"type": "start_stream",
"dataset_name": dataset_name,
"batch_size": 100 # Example batch size
}
await websocket.send(json.dumps(request))
print(f"Requested dataset: {dataset_name}")
# Receive data stream
try:
async for message in websocket:
# Decode base64 and decompress
decoded_data = base64.b64decode(message)
decompressed_data = gzip.decompress(decoded_data).decode('utf-8')
response = json.loads(decompressed_data)
if response.get("type") == "end_of_stream":
print("End of stream reached.")
break
elif "error" in response:
print(f"Error received: {response['error']}")
break
elif response.get("type") == "data_batch":
# Handle the received batch of data
batch_data = response.get("data", [])
print(f"Received batch with {len(batch_data)} points")
for point in batch_data:
print(point)
except websockets.exceptions.ConnectionClosed as e:
print(f"Connection closed: {e}")
finally:
print("WebSocket connection closed")
# Main entry point
async def main():
# List datasets
datasets = await fetch_datasets()
if datasets:
# Choose a random dataset name
dataset_name = random.choice(datasets)
print(f"Randomly selected dataset: {dataset_name}")
# Fetch and print the dataset in batches
await fetch_dataset(dataset_name)
else:
print("No datasets available.")
# Run the main function
if __name__ == "__main__":
asyncio.run(main())