-
Notifications
You must be signed in to change notification settings - Fork 19
/
udp_client.py
53 lines (44 loc) · 1.28 KB
/
udp_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
"""A sample client for the OpenBCI UDP server."""
import argparse
import cPickle as pickle
import json
import open_bci
import socket
parser = argparse.ArgumentParser(
description='Run a UDP client listening for streaming OpenBCI data.')
parser.add_argument(
'--json',
action='store_true',
help='Handle JSON data rather than pickled Python objects.')
parser.add_argument(
'--host',
help='The host to listen on.',
default='127.0.0.1')
parser.add_argument(
'--port',
help='The port to listen on.',
default='8888')
class UDPClient(object):
def __init__(self, ip, port, json):
self.ip = ip
self.port = port
self.json = json
self.client = socket.socket(
socket.AF_INET, # Internet
socket.SOCK_DGRAM)
self.client.bind((ip, port))
def start_listening(self, callback=None):
while True:
data, addr = self.client.recvfrom(1024)
if self.json:
sample = json.loads(data)
# In JSON mode we only recieve channel data.
print data
else:
sample = pickle.loads(data)
# Note that sample is an OpenBCISample object.
print sample.id
print sample.channels
args = parser.parse_args()
client = UDPClient(args.host, int(args.port), args.json)
client.start_listening()