#! /usr/bin/env python3
#-*- coding: utf-8 -*-

# python3 /home/scripts_python_tests_capteurs/UDP_server/AQCM05_UDP_client_1.py

import socket

# Create a UDP socket at client side
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPClientSocket.settimeout(0.5) 
bufferSize          = 1024

msgFromClient       = "AQCM05"
bytesToSend         = str.encode(msgFromClient)

failed = 0

serverAddressPort   = ("192.168.1.72", 31559)
try :
	# Send to server using created UDP socket
	UDPClientSocket.sendto(bytesToSend, serverAddressPort)
	msgFromServer = UDPClientSocket.recvfrom(bufferSize)
	message = msgFromServer[0].decode('utf-8') 
	if message == "192.168.1.72" :
		status = "OK"
	else :
		status = "!!!"
	
	print("192.168.1.72 : " + status)

except :
	print("192.168.1.72 : !")


serverAddressPort   = ("192.168.1.70", 31559)
try :
	# Send to server using created UDP socket
	UDPClientSocket.sendto(bytesToSend, serverAddressPort)
	msgFromServer = UDPClientSocket.recvfrom(bufferSize)
	message = msgFromServer[0].decode('utf-8') 
	if message == "192.168.1.70" :
		status = "OK"
	else :
		status = "!!!"
	
	print("192.168.1.70 : " + status)

except :
	print("192.168.1.70 : !")



serverAddressPort   = ("192.168.1.71", 31559)
try :
	# Send to server using created UDP socket
	UDPClientSocket.sendto(bytesToSend, serverAddressPort)
	msgFromServer = UDPClientSocket.recvfrom(bufferSize)
	message = msgFromServer[0].decode('utf-8') 
	if message == "192.168.1.71" :
		status = "OK"
	else :
		status = "!!!"
	
	print("192.168.1.71 : " + status)

except :
	print("192.168.1.71 : !")


try :
	serverAddressPort   = ("192.168.1.72", 31559)
	UDPClientSocket.sendto(bytesToSend, serverAddressPort)	
	msgFromServer = UDPClientSocket.recvfrom(bufferSize)
	message = msgFromServer[0].decode('utf-8') 
	if message == "192.168.1.72" :
		status = "OK"
	else :
		status = "!!!"
	
	print("192.168.1.72 : " + status)

except :
	print("192.168.1.72 : !")


