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