@@ -76,6 +76,88 | |||
|
76 | 76 | |
|
77 | 77 | return ipSource, ipDestino, cmd, data |
|
78 | 78 | |
|
79 | class TCPComm: | |
|
80 | ||
|
81 | __HEADER = "ABS" | |
|
82 | ||
|
83 | def __init__(self, ipSource, ipDestino, portDestino, asServer=False): | |
|
84 | ||
|
85 | self.ipSource = ipSource | |
|
86 | self.ipDestino = ipDestino | |
|
87 | self.portDestino = portDestino | |
|
88 | self.addr = (ipDestino,portDestino) | |
|
89 | self.addr2 = ("none",0) | |
|
90 | self.answer = "none" #test | |
|
91 | self.mode = "none" | |
|
92 | ||
|
93 | self.openSocket(asServer) | |
|
94 | ||
|
95 | def openSocket(self, asServer): | |
|
96 | ||
|
97 | #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM) | |
|
98 | self.socket_c = socket.socket() | |
|
99 | # self.socket_c.connect((self.ipDestino, self.portDestino)) | |
|
100 | ||
|
101 | if asServer: | |
|
102 | self.configAsServer() | |
|
103 | self.mode = "server" | |
|
104 | else: | |
|
105 | self.configAsClient() | |
|
106 | self.mode = "client" | |
|
107 | ||
|
108 | def configAsClient(self): | |
|
109 | #Buscar broadcast TCP | |
|
110 | # self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | |
|
111 | self.socket_c.connect(self.addr) | |
|
112 | ||
|
113 | def configAsServer(self): | |
|
114 | ||
|
115 | self.socket_c.bind(self.addr) | |
|
116 | self.socket_c.listen(1) | |
|
117 | self.addr2 = self.socket_c.accept() | |
|
118 | print "\nServer initialized" | |
|
119 | ||
|
120 | def waitData(self, nbytes = 16384): | |
|
121 | ||
|
122 | print "\nWaiting some data" | |
|
123 | if self.mode == "client": | |
|
124 | trama_rx, self.answer = self.socket_c.recv(nbytes) | |
|
125 | else: | |
|
126 | trama_rx, self.answer = self.addr2[0].recv(nbytes) | |
|
127 | ||
|
128 | print "\nThis socket has received some data from:" | |
|
129 | print self.answer | |
|
130 | ||
|
131 | ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx) | |
|
132 | ||
|
133 | return ipSource, ipDestino, cmd, data | |
|
134 | ||
|
135 | def sendData(self, cmd, data): | |
|
136 | ||
|
137 | if self.portDestino == 7000: | |
|
138 | trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":" | |
|
139 | else: | |
|
140 | trama_tx = data | |
|
141 | # Send messages | |
|
142 | if self.mode == "client": | |
|
143 | self.socket_c.send(trama_tx) | |
|
144 | else: | |
|
145 | self.addr2[0].send(trama_tx) | |
|
146 | print "Sending message:[" + trama_tx + "]" | |
|
147 | ||
|
148 | def __getTrama(self, trama): | |
|
149 | ||
|
150 | FrameList = trama.split(':') | |
|
151 | ||
|
152 | header = FrameList[0] | |
|
153 | ipSource = FrameList[1] | |
|
154 | ipDestino = FrameList[2] | |
|
155 | cmd = FrameList[3] | |
|
156 | data = FrameList[4] | |
|
157 | trash = FrameList[5] | |
|
158 | ||
|
159 | return ipSource, ipDestino, cmd, data | |
|
160 | ||
|
79 | 161 | #class FTPComm: |
|
80 | 162 | # |
|
81 | 163 | # ftp_servidor = 'ftp.servidor.com' |
General Comments 0
You need to be logged in to leave comments.
Login now