-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
53 lines (37 loc) · 1.16 KB
/
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
# define the library
import socket
import numpy as np
import encodings
# define the host and port
HOST = '127.0.0.1'
PORT = 65432
def random_data():
x1 = np.random.randint(0, 55, None)
y1 = np.random.randint(0, 45, None)
my_sensor = "{},{}".format(x1,y1)
return my_sensor
def my_server():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
print("Server started waiting for client to connect" )
s.bind((HOST, PORT))
s.listen(5)
conn, addr = s.accept()
with conn:
print('Connected By', addr)
while True:
data = conn.recv(1024).decode('utf-8')
if str(data) == "Data":
print("OK Sending Data ")
my_data = random_data()
x_encoded_data = my_data.encode('utf-8')
conn.sendall(x_encoded_data)
elif str(data) == "Quit":
print("Shutting down server ")
break
if not data:
break
else:
pass
if __name__ == ' __main__':
while 1:
my_server()