@@ -1,200 +1,200 | |||||
1 | import tftpy |
|
1 | #import tftpy | |
2 | import socket |
|
2 | import socket | |
3 |
|
3 | |||
4 | class UDPServer: |
|
4 | class UDPServer: | |
5 |
|
5 | |||
6 | buf = 16384 |
|
6 | buf = 16384 | |
7 | #buf = 1024 |
|
7 | #buf = 1024 | |
8 |
|
8 | |||
9 | def __init__(self, port): |
|
9 | def __init__(self, port): | |
10 | # Set the socket parameters |
|
10 | # Set the socket parameters | |
11 | host = "192.168.1.255" |
|
11 | host = "192.168.1.255" | |
12 | #host = "localhost" |
|
12 | #host = "localhost" | |
13 | self.addr = (host,port) |
|
13 | self.addr = (host,port) | |
14 | self.createObjects() |
|
14 | self.createObjects() | |
15 |
|
15 | |||
16 | def createObjects(self): |
|
16 | def createObjects(self): | |
17 |
|
17 | |||
18 | # Create socket and bind to address |
|
18 | # Create socket and bind to address | |
19 | self.sock_s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0) |
|
19 | self.sock_s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0) | |
20 | self.sock_s.bind(self.addr) |
|
20 | self.sock_s.bind(self.addr) | |
21 |
|
21 | |||
22 | def listen(self,mode): |
|
22 | def listen(self,mode): | |
23 |
|
23 | |||
24 | # Receive messages |
|
24 | # Receive messages | |
25 | print "Central Control initialized ...with UDP" |
|
25 | print "Central Control initialized ...with UDP" | |
26 |
|
26 | |||
27 | if mode == 0: |
|
27 | if mode == 0: | |
28 | while 1: |
|
28 | while 1: | |
29 | data_rx, add = self.sock_s.recvfrom(self.buf) |
|
29 | data_rx, add = self.sock_s.recvfrom(self.buf) | |
30 | if not data_rx: |
|
30 | if not data_rx: | |
31 | print "Client has exited!" |
|
31 | print "Client has exited!" | |
32 | break |
|
32 | break | |
33 |
|
33 | |||
34 | print "\nCentral Control:" |
|
34 | print "\nCentral Control:" | |
35 | print "Received message '", data_rx,"'" |
|
35 | print "Received message '", data_rx,"'" | |
36 | #print "Sendinf ack ..." |
|
36 | #print "Sendinf ack ..." | |
37 | #self.sock_s.sendto('ack', addr_rx) |
|
37 | #self.sock_s.sendto('ack', addr_rx) | |
38 | else: |
|
38 | else: | |
39 | data_rx, add = self.sock_s.recvfrom(self.buf) |
|
39 | data_rx, add = self.sock_s.recvfrom(self.buf) | |
40 | print "\nCentral Control:" |
|
40 | print "\nCentral Control:" | |
41 | print "Received message '", data_rx,"'" |
|
41 | print "Received message '", data_rx,"'" | |
42 |
|
42 | |||
43 | # Close socket |
|
43 | # Close socket | |
44 | self.sock_s.close() |
|
44 | self.sock_s.close() | |
45 | print "\nsocket closed" |
|
45 | print "\nsocket closed" | |
46 | return data_rx |
|
46 | return data_rx | |
47 |
|
47 | |||
48 | class UDPClient: |
|
48 | class UDPClient: | |
49 |
|
49 | |||
50 | buf = 1024 |
|
50 | buf = 1024 | |
51 |
|
51 | |||
52 | def __init__(self, port): |
|
52 | def __init__(self, port): | |
53 | # Set the socket parameters |
|
53 | # Set the socket parameters | |
54 | host = "192.168.1.255" |
|
54 | host = "192.168.1.255" | |
55 | self.multicast_addr = (host,port) |
|
55 | self.multicast_addr = (host,port) | |
56 | self.createObjects() |
|
56 | self.createObjects() | |
57 |
|
57 | |||
58 | def createObjects(self): |
|
58 | def createObjects(self): | |
59 |
|
59 | |||
60 | # Create socket |
|
60 | # Create socket | |
61 | self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0) |
|
61 | self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0) | |
62 | #if self.socket_c == -1: |
|
62 | #if self.socket_c == -1: | |
63 | # print "No se pudo establecer conexion con el socket" |
|
63 | # print "No se pudo establecer conexion con el socket" | |
64 |
|
64 | |||
65 | #self.socket_c.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20) |
|
65 | #self.socket_c.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20) | |
66 | self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) |
|
66 | self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | |
67 |
|
67 | |||
68 |
|
68 | |||
69 |
|
69 | |||
70 | def sendData(self,TxFrame): |
|
70 | def sendData(self,TxFrame): | |
71 |
|
71 | |||
72 | # Send messages |
|
72 | # Send messages | |
73 | if(self.socket_c.sendto(TxFrame, self.multicast_addr)): |
|
73 | if(self.socket_c.sendto(TxFrame, self.multicast_addr)): | |
74 | print "Sending message:[" + TxFrame + "]" |
|
74 | print "Sending message:[" + TxFrame + "]" | |
75 |
|
75 | |||
76 | data_rx, server_rx = self.socket_c.recvfrom(16) |
|
76 | data_rx, server_rx = self.socket_c.recvfrom(16) | |
77 |
|
77 | |||
78 | print "Data received ", data_rx, server_rx |
|
78 | print "Data received ", data_rx, server_rx | |
79 |
|
79 | |||
80 | # Close socket |
|
80 | # Close socket | |
81 | self.socket_c.close() |
|
81 | self.socket_c.close() | |
82 |
|
82 | |||
83 | class UDPComm: |
|
83 | class UDPComm: | |
84 |
|
84 | |||
85 | __HEADER = "ABS" |
|
85 | __HEADER = "ABS" | |
86 |
|
86 | |||
87 | def __init__(self, ipSource, ipDestino, portDestino, asServer=False): |
|
87 | def __init__(self, ipSource, ipDestino, portDestino, asServer=False): | |
88 |
|
88 | |||
89 | self.ipSource = ipSource |
|
89 | self.ipSource = ipSource | |
90 | self.ipDestino = ipDestino |
|
90 | self.ipDestino = ipDestino | |
91 | self.portDestino = portDestino |
|
91 | self.portDestino = portDestino | |
92 | self.addr = (ipDestino,portDestino) |
|
92 | self.addr = (ipDestino,portDestino) | |
93 |
|
93 | |||
94 | self.openSocket(asServer) |
|
94 | self.openSocket(asServer) | |
95 |
|
95 | |||
96 | def openSocket(self, asServer): |
|
96 | def openSocket(self, asServer): | |
97 |
|
97 | |||
98 | #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM) |
|
98 | #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM) | |
99 | self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0) |
|
99 | self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0) | |
100 | # self.socket_c.connect((self.ipDestino, self.portDestino)) |
|
100 | # self.socket_c.connect((self.ipDestino, self.portDestino)) | |
101 |
|
101 | |||
102 | if asServer: |
|
102 | if asServer: | |
103 | self.configAsServer() |
|
103 | self.configAsServer() | |
104 | else: |
|
104 | else: | |
105 | self.configAsClient() |
|
105 | self.configAsClient() | |
106 |
|
106 | |||
107 | def configAsClient(self): |
|
107 | def configAsClient(self): | |
108 | #Configurar broadcast |
|
108 | #Configurar broadcast | |
109 | self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) |
|
109 | self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | |
110 |
|
110 | |||
111 | def configAsServer(self): |
|
111 | def configAsServer(self): | |
112 |
|
112 | |||
113 | self.socket_c.bind(self.addr) |
|
113 | self.socket_c.bind(self.addr) | |
114 | print "\nServer initialized" |
|
114 | print "\nServer initialized" | |
115 |
|
115 | |||
116 | def waitRequest(self, nbytes): |
|
116 | def waitRequest(self, nbytes): | |
117 |
|
117 | |||
118 | trama_rx, add = self.socket_c.recvfrom(nbytes) |
|
118 | trama_rx, add = self.socket_c.recvfrom(nbytes) | |
119 | print "\nServer has received a data" |
|
119 | print "\nServer has received a data" | |
120 | ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx) |
|
120 | ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx) | |
121 |
|
121 | |||
122 | return ipSource, ipDestino, cmd, data |
|
122 | return ipSource, ipDestino, cmd, data | |
123 |
|
123 | |||
124 | def sendData(self,TxFrame): |
|
124 | def sendData(self,TxFrame): | |
125 |
|
125 | |||
126 | # Send messages |
|
126 | # Send messages | |
127 | if(self.socket_c.sendto(TxFrame, self.addr)): |
|
127 | if(self.socket_c.sendto(TxFrame, self.addr)): | |
128 | print "Sending message:[" + TxFrame + "]" |
|
128 | print "Sending message:[" + TxFrame + "]" | |
129 |
|
129 | |||
130 | data_rx, server_rx = self.socket_c.recvfrom(16) |
|
130 | # data_rx, server_rx = self.socket_c.recvfrom(16) | |
131 |
|
131 | # | ||
132 | print "Data received ", data_rx, server_rx |
|
132 | # print "Data received ", data_rx, server_rx | |
133 |
|
133 | |||
134 | # Close socket |
|
134 | # Close socket | |
135 | self.socket_c.close() |
|
135 | #self.socket_c.close() | |
136 |
|
136 | |||
137 | def sendTxRxCommand(self, cmd, data, nbytes = 16384): |
|
137 | def sendTxRxCommand(self, cmd, data, nbytes = 16384): | |
138 |
|
138 | |||
139 | self.sendRequest(cmd, data) |
|
139 | self.sendRequest(cmd, data) | |
140 |
|
140 | |||
141 | #time.sleep(1) |
|
141 | #time.sleep(1) | |
142 |
|
142 | |||
143 | # ipSource_rx, ipDestino_rx, cmd_rx, data_rx = self.getRpta(nbytes) |
|
143 | # ipSource_rx, ipDestino_rx, cmd_rx, data_rx = self.getRpta(nbytes) | |
144 |
|
144 | |||
145 | # if not(self.ipSource == ipDestino_rx): |
|
145 | # if not(self.ipSource == ipDestino_rx): | |
146 | # print "Error" |
|
146 | # print "Error" | |
147 |
|
147 | |||
148 | # return data_rx |
|
148 | # return data_rx | |
149 |
|
149 | |||
150 | def sendRequest(self, cmd, data): |
|
150 | def sendRequest(self, cmd, data): | |
151 |
|
151 | |||
152 | trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":" |
|
152 | trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":" | |
153 | #self.socket_c.send(trama_tx) |
|
153 | #self.socket_c.send(trama_tx) | |
154 | # Send messages |
|
154 | # Send messages | |
155 | if(self.socket_c.sendto(trama_tx, self.addr)): |
|
155 | if(self.socket_c.sendto(trama_tx, self.addr)): | |
156 | print "Sending message:[" + trama_tx + "]" |
|
156 | print "Sending message:[" + trama_tx + "]" | |
157 |
|
157 | |||
158 |
|
158 | |||
159 | def getRpta(self, nbytes): |
|
159 | def getRpta(self, nbytes): | |
160 |
|
160 | |||
161 | trama_rx = self.socket_c.recv(nbytes) |
|
161 | trama_rx = self.socket_c.recv(nbytes) | |
162 | ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx) |
|
162 | ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx) | |
163 |
|
163 | |||
164 | return ipSource, ipDestino, cmd, data |
|
164 | return ipSource, ipDestino, cmd, data | |
165 |
|
165 | |||
166 | def __getTrama(self, trama): |
|
166 | def __getTrama(self, trama): | |
167 |
|
167 | |||
168 | FrameList = trama.split(':') |
|
168 | FrameList = trama.split(':') | |
169 |
|
169 | |||
170 | header = FrameList[0] |
|
170 | header = FrameList[0] | |
171 | ipSource = FrameList[1] |
|
171 | ipSource = FrameList[1] | |
172 | ipDestino = FrameList[2] |
|
172 | ipDestino = FrameList[2] | |
173 | cmd = FrameList[3] |
|
173 | cmd = FrameList[3] | |
174 | data = FrameList[4] |
|
174 | data = FrameList[4] | |
175 | trash = FrameList[5] |
|
175 | trash = FrameList[5] | |
176 |
|
176 | |||
177 | return ipSource, ipDestino, cmd, data |
|
177 | return ipSource, ipDestino, cmd, data | |
178 |
|
178 | |||
179 | class FTPComm: |
|
179 | #class FTPComm: | |
180 |
|
180 | # | ||
181 | ftp_servidor = 'ftp.servidor.com' |
|
181 | # ftp_servidor = 'ftp.servidor.com' | |
182 | ftp_usuario = 'miusuario' |
|
182 | # ftp_usuario = 'miusuario' | |
183 | ftp_clave = 'miclave' |
|
183 | # ftp_clave = 'miclave' | |
184 | ftp_raiz = '/public_html' |
|
184 | # ftp_raiz = '/public_html' | |
185 |
|
185 | # | ||
186 | def __init__(self): |
|
186 | # def __init__(self): | |
187 |
|
187 | # | ||
188 | self.client = tftpy.TftpClient(self.ftp_servidor, '69') |
|
188 | # self.client = tftpy.TftpClient(self.ftp_servidor, '69') | |
189 |
|
189 | # | ||
190 |
|
190 | # | ||
191 | def sendFile(self, filename): |
|
191 | # def sendFile(self, filename): | |
192 |
|
192 | # | ||
193 | self.client.upload(filename) |
|
193 | # self.client.upload(filename) | |
194 |
|
194 | # | ||
195 | if __name__ == '__main__': |
|
195 | #if __name__ == '__main__': | |
196 |
|
196 | # | ||
197 | obj = FTPComm() |
|
197 | # obj = FTPComm() | |
198 |
|
198 | |||
199 |
|
199 | |||
200 | No newline at end of file |
|
200 |
General Comments 0
You need to be logged in to leave comments.
Login now