-
Notifications
You must be signed in to change notification settings - Fork 0
/
socktest.py
executable file
·69 lines (58 loc) · 1.68 KB
/
socktest.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
#!/usr/bin/env python
import json
import socket
import sys
HOST, PORT = "127.0.0.1", 7777
def main(argv):
if len(argv) < 1:
print('main.py <server ip address>')
sys.exit(2)
else:
HOST = argv[0]
# Initialize network connection here
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a socket (SOCK_STREAM means a TCP socket)
try:
sock.connect((HOST, PORT))
except socket.error, msg:
print("Could not connect to server! Error: %s" % msg)
sys.exit(-1)
# Main update loop
running = True
while running == True:
# Send the result to iPhone
'''
Wrap processed data as json
id: message id
cnt: packet counter
cat: category of sound
int: intensity of sound
cont: content of a sound
dir: direction of the sound
isFinal: a boolean terminal indicator
'''
# A container for processed data
data = {}
# Temporary test data
ID = 2
cnt = 1
cat = 'SPEECH'
inte = 3.0
direc = 'BACKLEFT'
try:
cont = raw_input("Input data to send to server: ")
except KeyboardInterrupt:
print("Ctrl+C pressed. Client terminating...")
running = False
data['id'] = ID
data['cnt'] = cnt
data['cat'] = cat
data['int'] = inte
data['cont'] = cont
data['dir'] = direc
data['isFinal'] = True
data = json.dumps(data)
sock.sendall(bytes(data + "\n"))
# Shutdown network connection
sock.close()
if __name__ == '__main__':
main(sys.argv[1:])