forked from IlliniHyperloopComputing/Pod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDPClient.py
More file actions
41 lines (34 loc) · 1 KB
/
UDPClient.py
File metadata and controls
41 lines (34 loc) · 1 KB
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
import socket
import json as js
from time import time
from random import random
from flask import Flask, render_template, make_response
UDP_IP = "192.168.7.1"
UDP_PORT = 5005
SENDER_UDP_IP = "192.168.7.2"
SENDER_UDP_PORT = 8008
BUFFER_SIZE = 1024
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template('index.html', data='test')
@app.route('/live-data')
def live_data():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((UDP_IP, UDP_PORT))
count = 0
data, addr = sock.recvfrom(BUFFER_SIZE)
print ("asdasdasd")
if data:
print "received message:", js.loads(data)
sock.sendto(js.dumps(count), (SENDER_UDP_IP, SENDER_UDP_PORT))
#sock.close();
data = js.loads(data)
data = data[2]
data2 = [time() * 1000, data]
response = make_response(js.dumps(data2))
response.content_type = 'application/json'
return response
if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1', port=5000)