forked from VedantParanjape/pid-tuning-gui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
udp_server.py
64 lines (52 loc) · 1.66 KB
/
udp_server.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
# This example code is in the Public Domain (or CC0 licensed, at your option.)
# Unless required by applicable law or agreed to in writing, this
# software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied.
# -*- coding: utf-8 -*-
import socket
import sys
import json
import datetime, time
# ----------- Config ----------
IP_VERSION = 'IPv4'
PORT = 1212
# -------------------------------
if IP_VERSION == 'IPv4':
family_addr = socket.AF_INET
elif IP_VERSION == 'IPv6':
family_addr = socket.AF_INET6
else:
print('IP_VERSION must be IPv4 or IPv6')
sys.exit(1)
try:
server_sock = socket.socket(family_addr, socket.SOCK_DGRAM)
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except socket.error as msg:
print('Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit()
try:
server_sock.bind(('', PORT))
except socket.error as msg:
print('Bind failed. Error: ' + str(msg[0]) + ': ' + msg[1])
sys.exit()
st_time = 0
i = 0
while True:
try:
i = i + 1
print(i)
data, addr = server_sock.recvfrom(150)
if i == 1:
st_time = datetime.datetime.now()
if i==100:
end_time = datetime.datetime.now()
diff = end_time - st_time
print(diff.seconds, ".", diff.microseconds)
time.sleep(3)
if not data:
break
data = data.decode()
print('Reply[' + addr[0] + ':' + str(addr[1]) + '] - ' + data)
except socket.error as msg:
print('Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
server_sock.close()