Exemple #1
0
    def run(self):
        # Dictionary to deal with queue for multiples threads
        d = {}
        d['ip'] = self.m['ip']
        d['index'] = self.index
        d['respostas'] = []

        # For each command from each machine, create a socket connection
        for cmd in self.m['cmds']:
            arg = form.getfirst(self.m['ip'] + "_arg" + str(cmd))

            try:
                # Creates a TCP/IP socket
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.settimeout(14)
                # Connects the socket to the machine listening door
                server_address = (self.m['ip'], self.m['porta'])
                sock.connect(server_address)
                # Check is there is any arg to be sent
                if arg is None:
                    arg = ''
                header = "REQUEST " + cmd + " " + arg
                sock.sendall(unidecode(header.decode()))
                # Wait for answer from daemon
                resposta = sock.recv(65536)
                # Eg.: Response 1 "answer"
                try:
                    # Eg.: cmd = 1 saida ="answer"
                    cmd, saida = resposta.split(None, 2)[1:]
                except:
                    cmd = ''
                    saida = ''
                # Put answer from server in dicionary
                d['respostas'].append(["Maquina: " + self.m['ip'] +
                                       ", Comando: " + cmd_name(cmd), saida])
            except Exception:
                pass
            finally:
                sock.close()

        # Put dictionary in queue
        self.q.put(d)
Exemple #2
0
    def run(self):
        try:
            while True:
                message = self.socket.recv(2**16).decode()
                message = re.sub(r"\r\n", "", message)

                if not message:
                    print("Empty message. Fechando o socket")
                    self.socket.close()
                    break

                print("Recebi de '{}': '{}'".format(self.address[0], message))

                # Eg.: message = "REQUEST 1 -j"
                try:
                    tipo, cmd, arg = message.split(' ', 2)

                # Eg.: message = "REQUEST 1"
                except:
                    try:
                        tipo, cmd = message.split(' ')
                        arg = ''

                    # Eg.: message = "REQUEST"
                    except:
                        header = "HTTP/1.1 400 Bad Request\r\n" \
                                 "Connection: close\r\n\r\n"
                        print("Enviando `{}` para {}".format(
                            header, self.address[0]))
                        self.socket.sendall(unidecode(header.decode()))
                        print("Fechando o socket")
                        self.socket.close()
                        break

                print("Tipo: '{}', cmd: '{}', arg: '{}'".format(
                    tipo, cmd, arg))

                # Only request method allowed
                if tipo == 'REQUEST':
                    full_cmd = cmd_name(cmd)
                    # Verify if there is any argument
                    if arg:
                        full_cmd += " " + self.clean_arg(arg)
                    print("Rodando: `{}`".format(full_cmd))
                    try:
                        saida = check_output(full_cmd,
                                             stderr=STDOUT,
                                             shell=True)
                    except CalledProcessError, e:
                        saida = e.output
                    header = "RESPONSE " + cmd + " " + saida
                    print("Enviando `{}` para {}".format(
                        header, self.address[0]))
                    self.socket.sendall(unidecode(header.decode()))

                # Returns 405 if receive a GET, POST, HEAD, PUT, OPTIONS, etc
                # Could also be 501 Method Not Implemented
                else:
                    header = "HTTP/1.1 405 Method Not Allowed\r\n" \
                             "Allow: REQUEST\r\n" \
                             "Connection: close\r\n\r\n"
                    print("Enviando `{}` para {}".format(
                        header, self.address[0]))
                    self.socket.sendall(unidecode(header.decode()))
                    print("Fechando o socket")
                    self.socket.close()
                    break

        except Exception, e:
            print("Error: '{}'".format(e))
            print("Fechando o socket")
            self.socket.close()
Exemple #3
0
    def run(self):
        try:
            while True:
                message = self.socket.recv(2 ** 16).decode()
                message = re.sub(r"\r\n", "", message)

                if not message:
                    print("Empty message. Fechando o socket")
                    self.socket.close()
                    break

                print("Recebi de '{}': '{}'".format(self.address[0], message))

                # Eg.: message = "REQUEST 1 -j"
                try:
                    tipo, cmd, arg = message.split(" ", 2)

                # Eg.: message = "REQUEST 1"
                except:
                    try:
                        tipo, cmd = message.split(" ")
                        arg = ""

                    # Eg.: message = "REQUEST"
                    except:
                        header = "HTTP/1.1 400 Bad Request\r\n" "Connection: close\r\n\r\n"
                        print("Enviando `{}` para {}".format(header, self.address[0]))
                        self.socket.sendall(unidecode(header.decode()))
                        print("Fechando o socket")
                        self.socket.close()
                        break

                print("Tipo: '{}', cmd: '{}', arg: '{}'".format(tipo, cmd, arg))

                # Only request method allowed
                if tipo == "REQUEST":
                    full_cmd = cmd_name(cmd)
                    # Verify if there is any argument
                    if arg:
                        full_cmd += " " + self.clean_arg(arg)
                    print("Rodando: `{}`".format(full_cmd))
                    try:
                        saida = check_output(full_cmd, stderr=STDOUT, shell=True)
                    except CalledProcessError, e:
                        saida = e.output
                    header = "RESPONSE " + cmd + " " + saida
                    print("Enviando `{}` para {}".format(header, self.address[0]))
                    self.socket.sendall(unidecode(header.decode()))

                # Returns 405 if receive a GET, POST, HEAD, PUT, OPTIONS, etc
                # Could also be 501 Method Not Implemented
                else:
                    header = "HTTP/1.1 405 Method Not Allowed\r\n" "Allow: REQUEST\r\n" "Connection: close\r\n\r\n"
                    print("Enviando `{}` para {}".format(header, self.address[0]))
                    self.socket.sendall(unidecode(header.decode()))
                    print("Fechando o socket")
                    self.socket.close()
                    break

        except Exception, e:
            print("Error: '{}'".format(e))
            print("Fechando o socket")
            self.socket.close()