|
|
import tftpy
|
|
|
import socket
|
|
|
|
|
|
class UDPServer:
|
|
|
|
|
|
buf = 16384
|
|
|
#buf = 1024
|
|
|
|
|
|
def __init__(self, port):
|
|
|
# Set the socket parameters
|
|
|
host = "192.168.1.255"
|
|
|
#host = "localhost"
|
|
|
self.addr = (host,port)
|
|
|
self.createObjects()
|
|
|
|
|
|
def createObjects(self):
|
|
|
|
|
|
# Create socket and bind to address
|
|
|
self.sock_s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0)
|
|
|
self.sock_s.bind(self.addr)
|
|
|
|
|
|
def listen(self,mode):
|
|
|
|
|
|
# Receive messages
|
|
|
print "Central Control initialized ...with UDP"
|
|
|
print
|
|
|
if mode == 0:
|
|
|
while 1:
|
|
|
data_rx, add = self.sock_s.recvfrom(self.buf)
|
|
|
if not data_rx:
|
|
|
print "Client has exited!"
|
|
|
break
|
|
|
|
|
|
print "\nCentral Control:"
|
|
|
print "Received message '", data_rx,"'"
|
|
|
#print "Sendinf ack ..."
|
|
|
#self.sock_s.sendto('ack', addr_rx)
|
|
|
else:
|
|
|
data_rx, add = self.sock_s.recvfrom(self.buf)
|
|
|
print "\nCentral Control:"
|
|
|
print "Received message '", data_rx,"'"
|
|
|
|
|
|
# Close socket
|
|
|
self.sock_s.close()
|
|
|
print "\nsocket closed"
|
|
|
return data_rx
|
|
|
|
|
|
class UDPClient:
|
|
|
|
|
|
buf = 1024
|
|
|
|
|
|
def __init__(self, port):
|
|
|
# Set the socket parameters
|
|
|
host = "192.168.1.255"
|
|
|
self.multicast_addr = (host,port)
|
|
|
self.createObjects()
|
|
|
|
|
|
def createObjects(self):
|
|
|
|
|
|
# Create socket
|
|
|
self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0)
|
|
|
#if self.socket_c == -1:
|
|
|
# print "No se pudo establecer conexion con el socket"
|
|
|
|
|
|
#self.socket_c.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20)
|
|
|
self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
|
|
|
|
|
|
|
|
|
|
|
def sendData(self,TxFrame):
|
|
|
|
|
|
# Send messages
|
|
|
if(self.socket_c.sendto(TxFrame, self.multicast_addr)):
|
|
|
print "Sending message:[" + TxFrame + "]"
|
|
|
|
|
|
data_rx, server_rx = self.socket_c.recvfrom(16)
|
|
|
|
|
|
print "Data received ", data_rx, server_rx
|
|
|
|
|
|
# Close socket
|
|
|
self.socket_c.close()
|
|
|
|
|
|
class UDPComm:
|
|
|
|
|
|
__HEADER = "ABS"
|
|
|
|
|
|
def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
|
|
|
|
|
|
self.ipSource = ipSource
|
|
|
self.ipDestino = ipDestino
|
|
|
self.portDestino = portDestino
|
|
|
self.addr = (ipDestino,portDestino)
|
|
|
|
|
|
self.openSocket(asServer)
|
|
|
|
|
|
def openSocket(self, asServer):
|
|
|
|
|
|
#self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
|
|
|
self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0)
|
|
|
# self.socket_c.connect((self.ipDestino, self.portDestino))
|
|
|
|
|
|
if asServer:
|
|
|
self.configAsServer()
|
|
|
else:
|
|
|
self.configAsClient()
|
|
|
|
|
|
def configAsClient(self):
|
|
|
#Configurar broadcast
|
|
|
self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
|
|
|
|
|
def configAsServer(self):
|
|
|
|
|
|
self.socket_c.bind(self.addr)
|
|
|
print "\nServer initialized"
|
|
|
|
|
|
def waitRequest(self, nbytes):
|
|
|
|
|
|
trama_rx, add = self.socket_c.recvfrom(nbytes)
|
|
|
print "\nServer has received a data"
|
|
|
ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
|
|
|
|
|
|
return ipSource, ipDestino, cmd, data
|
|
|
|
|
|
def sendData(self,TxFrame):
|
|
|
|
|
|
# Send messages
|
|
|
if(self.socket_c.sendto(TxFrame, self.addr)):
|
|
|
print "Sending message:[" + TxFrame + "]"
|
|
|
|
|
|
data_rx, server_rx = self.socket_c.recvfrom(16)
|
|
|
|
|
|
print "Data received ", data_rx, server_rx
|
|
|
|
|
|
# Close socket
|
|
|
self.socket_c.close()
|
|
|
|
|
|
def sendTxRxCommand(self, cmd, data, nbytes = 16384):
|
|
|
|
|
|
self.sendRequest(cmd, data)
|
|
|
|
|
|
#time.sleep(1)
|
|
|
|
|
|
# ipSource_rx, ipDestino_rx, cmd_rx, data_rx = self.getRpta(nbytes)
|
|
|
|
|
|
# if not(self.ipSource == ipDestino_rx):
|
|
|
# print "Error"
|
|
|
|
|
|
# return data_rx
|
|
|
|
|
|
def sendRequest(self, cmd, data):
|
|
|
|
|
|
trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":"
|
|
|
#self.socket_c.send(trama_tx)
|
|
|
# Send messages
|
|
|
if(self.socket_c.sendto(trama_tx, self.addr)):
|
|
|
print "Sending message:[" + trama_tx + "]"
|
|
|
|
|
|
|
|
|
def getRpta(self, nbytes):
|
|
|
|
|
|
trama_rx = self.socket_c.recv(nbytes)
|
|
|
ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
|
|
|
|
|
|
return ipSource, ipDestino, cmd, data
|
|
|
|
|
|
def __getTrama(self, trama):
|
|
|
|
|
|
FrameList = trama.split(':')
|
|
|
|
|
|
header = FrameList[0]
|
|
|
ipSource = FrameList[1]
|
|
|
ipDestino = FrameList[2]
|
|
|
cmd = FrameList[3]
|
|
|
data = FrameList[4]
|
|
|
trash = FrameList[5]
|
|
|
|
|
|
return ipSource, ipDestino, cmd, data
|
|
|
|
|
|
class FTPComm:
|
|
|
|
|
|
ftp_servidor = 'ftp.servidor.com'
|
|
|
ftp_usuario = 'miusuario'
|
|
|
ftp_clave = 'miclave'
|
|
|
ftp_raiz = '/public_html'
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
self.client = tftpy.TftpClient(self.ftp_servidor, '69')
|
|
|
|
|
|
|
|
|
def sendFile(self, filename):
|
|
|
|
|
|
self.client.upload(filename)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
obj = FTPComm()
|
|
|
|
|
|
|
|
|
|