-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
49 lines (44 loc) · 1.55 KB
/
Copy pathserver.py
File metadata and controls
49 lines (44 loc) · 1.55 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
42
43
44
45
46
47
48
49
#!/usr/bin/python
import socket
def parseMsg(strMsg):
_strMsg = strMsg.upper()
msgArr = _strMsg.split(":")
if(len(msgArr) < 2):
print("Warn: Unkown message type!")
else:
val = float(msgArr[1])
# print(f"{msgArr[0]}:{val}")
## runCtrlLogic(msgArr[0], msgArr[1]) # implemented directly below
if(_strMsg.startswith("F")):
print("F")
elif(_strMsg.startswith("R")):
print("R")
elif(_strMsg.startswith("L")):
print("L")
else:
print("Error: Unknown message head!")
def start_udp_server():
HOST = "127.0.0.1"
PORT = 9393
BUFFER = 1024
# Create a datagram socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Bind to address and ip
UDPServerSocket.bind((HOST, PORT))
print("UDP server up and listening")
# Listen for incoming datagrams
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(BUFFER)
message = bytesAddressPair[0]
address = bytesAddressPair[1]
clientMsg = message.decode()
_clientMsg = "Message from Client: {}".format(clientMsg)
_clientIP = "Client IP Address: {}".format(address)
print(_clientMsg)
print(_clientIP)
parseMsg(clientMsg) # exact logic for control
# Send a reply to client
msgFromServer = "Client state running on: {}".format(address)
bytesToSend = str.encode(msgFromServer)
UDPServerSocket.sendto(bytesToSend, address)
start_udp_server()