forked from adafruit/Adafruit-Raspberry-Pi-Python-Code
/
client.py
53 lines (37 loc) · 1.24 KB
/
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
import socket
import sys
import threading
import datetime
import time
from server import server
# make ctrl-c kill the program
import signal
import sys
def signal_handler(signal, frame):
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
class Communication():
def __init__(self, sock, *args, **kwargs):
self.sock = sock
def set(self, message):
print 'echo %s' % message
sock.sendall(message) # echo all messages
class ServerThread(threading.Thread):
def __init__(self, communication, *args, **kwargs):
self.communication = communication
super(ServerThread, self).__init__(*args, **kwargs)
def run(self):
server('localhost', 3001, communication=self.communication)
if __name__ == '__main__':
# Create a socket (SOCK_STREAM means a TCP socket)
# client of puredata: use 'netreceive 3000' in pd
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 3000))
communication = Communication(sock=sock)
server_thread = ServerThread(communication=communication)
server_thread.daemon = True
server_thread.start()
sock.sendall('0;')
# TODO: handle events from input device
while True:
time.sleep(1)