##// END OF EJS Templates
imanay -
r60:61
parent child
Show More
@@ -1,100 +1,100
1 #import tftpy
1 #import tftpy
2 import socket
2 import socket
3
3
4 class UDPComm:
4 class UDPComm:
5
5
6 __HEADER = "ABS"
6 __HEADER = "ABS"
7
7
8 def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
8 def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
9
9
10 self.ipSource = ipSource
10 self.ipSource = ipSource
11 self.ipDestino = ipDestino
11 self.ipDestino = ipDestino
12 self.portDestino = portDestino
12 self.portDestino = portDestino
13 self.addr = (ipDestino,portDestino)
13 self.addr = (ipDestino,portDestino)
14 self.answer = ipDestino #test
14 self.answer = "none" #test
15 self.mode = "none"
15 self.mode = "none"
16
16
17 self.openSocket(asServer)
17 self.openSocket(asServer)
18
18
19 def openSocket(self, asServer):
19 def openSocket(self, asServer):
20
20
21 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
21 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
22 self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0)
22 self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0)
23 # self.socket_c.connect((self.ipDestino, self.portDestino))
23 # self.socket_c.connect((self.ipDestino, self.portDestino))
24
24
25 if asServer:
25 if asServer:
26 self.configAsServer()
26 self.configAsServer()
27 self.mode = "server"
27 self.mode = "server"
28 else:
28 else:
29 self.configAsClient()
29 self.configAsClient()
30 self.mode = "client"
30 self.mode = "client"
31
31
32 def configAsClient(self):
32 def configAsClient(self):
33 #Configurar broadcast
33 #Configurar broadcast
34 self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
34 self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
35
35
36 def configAsServer(self):
36 def configAsServer(self):
37
37
38 self.socket_c.bind(self.addr)
38 self.socket_c.bind(self.addr)
39 print "\nServer initialized"
39 print "\nServer initialized"
40
40
41 def waitData(self, nbytes = 16384):
41 def waitData(self, nbytes = 16384):
42
42
43 print "\nWaiting some data"
43 print "\nWaiting some data"
44 trama_rx, self.answer = self.socket_c.recvfrom(nbytes)
44 trama_rx, self.answer = self.socket_c.recvfrom(nbytes)
45 print "\nThis socket has received some data from:"
45 print "\nThis socket has received some data from:"
46 print self.answer
46 print self.answer
47 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
47 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
48
48
49 return ipSource, ipDestino, cmd, data
49 return ipSource, ipDestino, cmd, data
50
50
51 def sendData(self, cmd, data):
51 def sendData(self, cmd, data):
52
52
53 if self.portDestino == 7000:
53 if self.portDestino == 7000:
54 trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":"
54 trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":"
55 else:
55 else:
56 trama_tx = data
56 trama_tx = data
57
57
58 if self.mode == "client":
58 if self.mode == "client":
59 destiny = self.addr
59 destiny = self.addr
60 else:
60 else:
61 destiny = self.answer
61 destiny = self.answer
62 # Send messages
62 # Send messages
63 if(self.socket_c.sendto(trama_tx, destiny)):
63 if(self.socket_c.sendto(trama_tx, destiny)):
64 print "Sending message:[" + trama_tx + "] to " + str(destiny)
64 print "Sending message:[" + trama_tx + "] to " + str(destiny)
65
65
66 def __getTrama(self, trama):
66 def __getTrama(self, trama):
67
67
68 FrameList = trama.split(':')
68 FrameList = trama.split(':')
69
69
70 header = FrameList[0]
70 header = FrameList[0]
71 ipSource = FrameList[1]
71 ipSource = FrameList[1]
72 ipDestino = FrameList[2]
72 ipDestino = FrameList[2]
73 cmd = FrameList[3]
73 cmd = FrameList[3]
74 data = FrameList[4]
74 data = FrameList[4]
75 trash = FrameList[5]
75 trash = FrameList[5]
76
76
77 return ipSource, ipDestino, cmd, data
77 return ipSource, ipDestino, cmd, data
78
78
79 #class FTPComm:
79 #class FTPComm:
80 #
80 #
81 # ftp_servidor = 'ftp.servidor.com'
81 # ftp_servidor = 'ftp.servidor.com'
82 # ftp_usuario = 'miusuario'
82 # ftp_usuario = 'miusuario'
83 # ftp_clave = 'miclave'
83 # ftp_clave = 'miclave'
84 # ftp_raiz = '/public_html'
84 # ftp_raiz = '/public_html'
85 #
85 #
86 # def __init__(self):
86 # def __init__(self):
87 #
87 #
88 # self.client = tftpy.TftpClient(self.ftp_servidor, '69')
88 # self.client = tftpy.TftpClient(self.ftp_servidor, '69')
89 #
89 #
90 #
90 #
91 # def sendFile(self, filename):
91 # def sendFile(self, filename):
92 #
92 #
93 # self.client.upload(filename)
93 # self.client.upload(filename)
94 #
94 #
95 #if __name__ == '__main__':
95 #if __name__ == '__main__':
96 #
96 #
97 # obj = FTPComm()
97 # obj = FTPComm()
98
98
99
99
100 No newline at end of file
100
General Comments 0
You need to be logged in to leave comments. Login now