-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_server.py
More file actions
38 lines (28 loc) · 803 Bytes
/
socket_server.py
File metadata and controls
38 lines (28 loc) · 803 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
#!/usr/bin/env python3
import socket
import numpy as np
import json
import time
import random
HOST = '192.168.11.18' # Standard loopback interface address (localhost)
PORT = 65431 # Port to listen on (non-privileged ports are > 1023)
sample = 0
def get_graph_data():
global sample
sample += 1
graph_to_send = json.dumps({
'x':random.uniform(-1,1),
'y':random.uniform(-1,1),
'z':random.uniform(-1,1),
's':sample
})
return graph_to_send
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
conn.sendall(get_graph_data().encode('utf-8'))