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