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