-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_server.py
44 lines (40 loc) · 1.11 KB
/
test_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
import socket
import urllib.request
import time
# bind all IP
HOST = '0.0.0.0'
# Listen on Port
PORT = 2255
SEND_PING_PORT = 10211
#Size of receive buffer
BUFFER_SIZE = 1024
# Create a TCP/IP socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the host and port
s.bind((HOST, PORT))
collar_ip = ""
collar_token =""
while True:
# Receive BUFFER_SIZE bytes data
# data is a list with 2 elements
# first is data
#second is client address
data = s.recvfrom(BUFFER_SIZE)
if data:
collar_ip = data[1][0]
collar_token = str(data[0]).split('|')[-1]
print('discovery collar IP:' , collar_ip, "token Id:", collar_token)
time.sleep(0.2)
s.sendto(bytes("hello collar from server 101", "utf-8"), (collar_ip, SEND_PING_PORT))
try:
contents = urllib.request.urlopen("http://"+collar_ip+"/pedometer").read()
break
except:
pass
# Close connection
s.close()
# getData
while True:
contents = urllib.request.urlopen("http://"+collar_ip+"/pedometer").read()
print(contents)
time.sleep(10)