def do_GET(self):
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        # Print the request line
        termcolor.cprint(self.requestline, 'green')
        termcolor.cprint(self.path, "blue")

        o = urlparse(self.path)
        path_name = o.path
        arguments = parse_qs(o.query)
        print("Resource requested: ", path_name)
        print("Arguments: ", arguments)

        context = {}

        if path_name == "/":
            context["n_sequences"] = len(SEQUENCES_LIST)
            context["genes_list"] = GENES_LIST
            contents = su.read_template_html_file("./html/index.html").render(
                context=context)

        elif path_name == "/ping":
            contents = su.read_template_html_file("./html/ping.html").render()

        elif path_name == "/get":
            number_sequence = arguments["sequence"][0]
            contents = su.get(number_sequence, SEQUENCES_LIST)

        elif path_name == "/gene":
            gene = arguments["gene"][0]
            contents = su.gene(gene)

        elif path_name == "/operation":
            sequence = arguments["sequence"][0]
            operation = arguments["operation"][0]
            if operation == "info":
                contents = su.info(sequence)
            elif operation == "comp":
                contents = su.comp(sequence)
            else:
                contents = su.rev(sequence)

        else:
            contents = su.read_template_html_file("./html/error.html").render()

        # Generating the response message
        self.send_response(200)  # -- Status line: OK!

        # Define the content-type header:
        self.send_header('Content-Type', 'text/html')
        self.send_header('Content-Length', str(len(contents.encode())))

        # The header is finished
        self.end_headers()

        # Send the response message
        self.wfile.write(contents.encode())
Esempio n. 2
0
    def do_GET(self):
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        # Print the request line
        termcolor.cprint(self.requestline, 'green')
        termcolor.cprint(self.path, 'blue')

        o = urlparse(self.path)
        path_name = o.path
        arguments = parse_qs(o.query)
        print("Resource requested: ", path_name)
        print("Parameters:", arguments)
        context = {}
        if path_name == "/":
            context["n_sequences"] = len(LIST_SEQUENCES)
            context["list_genes"] = LIST_GENES
            contents = su.read_template_html_file("./HTML/INDEX.html").render(
                context=context)
        elif path_name == "/ping":
            contents = su.read_template_html_file("./HTML/ping.html").render()
        elif path_name == "/get":
            number_sequence = arguments["sequence"][0]
            contents = su.get(LIST_SEQUENCES, number_sequence)
        elif path_name == "/gene":
            gene = arguments["gene"][0]
            contents = su.gene(gene)
        elif path_name == "/operation":
            try:
                sequence = arguments["sequence"][0]
                operation = arguments["calculation"][0]
                if operation == "Rev":
                    contents = su.rev(sequence)
                elif operation == "Comp":
                    contents = su.comp(sequence)
                elif operation == "Info":
                    contents = su.info(sequence)
            except KeyError:
                contents = su.read_template_html_file(
                    "./HTML/error_operation.html").render()
        else:
            contents = su.read_template_html_file("./HTML/ERROR.html").render()

        # Generating the response message
        self.send_response(200)  # -- Status line: OK!

        # Define the content-type header:
        self.send_header('Content-Type', 'text/html')
        self.send_header('Content-Length', len(contents.encode()))

        # The header is finished
        self.end_headers()

        # Send the response message
        self.wfile.write(contents.encode())

        return
Esempio n. 3
0
    def do_GET(self):
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        # Print the request line
        termcolor.cprint(self.requestline, 'green')
        termcolor.cprint(self.path, 'blue')

        o = urlparse(self.path)
        path_name = o.path
        arguments = parse_qs(o.query)
        print("Resource requested: ", path_name)
        print("Parameters:", arguments)

        # IN this simple server version:
        # We are NOT processing the client's request
        # It is a happy server: It always returns a message saying
        # that everything is ok
        context = {}
        if path_name == "/":
            context["n_sequences"] = len(LIST_SEQUENCES)
            context["list_genes"] = LIST_GENES
            contents = su.read_template_html_file("./html/index.html").render(
                context=context)
        elif path_name == "/test":
            contents = su.read_template_html_file("./html/test.html").render()
        elif path_name == "/ping":
            contents = su.read_template_html_file("./html/ping.html").render()
        elif path_name == "/get":
            number_sequence = arguments["sequence"][0]
            contents = su.get(LIST_SEQUENCES, number_sequence)
        elif path_name == "/operation":
            sequence = arguments["sequence"][0]
            operation = arguments["sequence"][0]
            if operation == "info":
                contents = su.info(sequence)
            elif operation == "comp":
                contents = su.comp(sequence)
            else:
                contents = su.rev(sequence)
        elif path_name == "/gene":
            gene = arguments["gene"][0]
            contents = su.gene(gene)
        else:
            contents = su.read_template_html_file("./html/error.html").render()

        # Generating the response message
        self.send_response(200)  # -- Status line: OK!
        # Define the content-type header:
        self.send_header('Content-Type', 'text/html')
        self.send_header('Content-Length', str(len(contents.encode())))
        # The header is finished
        self.end_headers()
        # Send the response message
        self.wfile.write(contents.encode())
        return
Esempio n. 4
0
    def do_GET(self):
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        # Print the request line
        print(self.requestline)
        print(self.path)

        o = urlparse(self.path)  #we create a urlparse object
        path_name = o.path  #create a path for the object, it will be like self.path but without the question mark
        arguments = parse_qs(o.query)
        print("Resource requested: ", path_name)
        print("Parameters:", arguments)

        context = {}
        if path_name == "/":
            context["n_sequences"] = len(LIST_SEQUENCES)
            context["list_genes"] = LIST_GENES
            contents = su.read_template_html_file("./html/index.html").render(
                context=context)
        elif path_name == "/test":
            contents = su.read_template_html_file("./html/test.html").render()
        elif path_name == "/ping":
            contents = su.read_template_html_file("./html/ping.html").render()
        elif path_name == "/get":
            number_sequence = arguments["sequence"][0]
            contents = su.get(LIST_SEQUENCES, number_sequence)
        elif path_name == "/gene":
            gene = arguments["gene"][0]
            contents = su.gene(gene)
        elif path_name == "/operation":
            try:
                if arguments['calculation'][0] == 'Rev':
                    seq = arguments['sequence'][0]
                    contents = su.rev(seq)
                elif arguments['calculation'][0] == 'Info':
                    seq = arguments['sequence'][0]
                    contents = su.info(seq)
                elif arguments['calculation'][0] == 'Comp':
                    seq = arguments['sequence'][0]
                    contents = su.comp(seq)
            except KeyError:
                contents = su.read_template_html_file(
                    "./html/error.html").render()
        else:
            contents = su.read_template_html_file("./html/error.html").render()

        # Generating the response message
        self.send_response(200)
        self.send_header('Content-Type', 'text/html')
        self.send_header('Content-Length', len(contents.encode()))
        # The header is finished
        self.end_headers()
        # Send the response message
        self.wfile.write(contents.encode())
        return
Esempio n. 5
0
    def do_GET(self):
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        termcolor.cprint(self.requestline, 'green')
        termcolor.cprint(self.path, "blue")

        o = urlparse(self.path)
        path_name = o.path
        arguments = parse_qs(o.query)
        print("Resource requested:", path_name)
        print("Parameters:", arguments)

        context = {}
        contents = None
        if path_name == "/":
            context["n_seq"] = len(LIST_SEQ)
            context["list_genes"] = LIST_GENES
            context["ops"] = LIST_OPS
            contents = utils.read_template(HTMLS + 'form1.html').render(
                context=context)
        elif path_name == "/ping":
            contents = utils.read_template(HTMLS + 'ping.html').render()
        elif path_name == "/get":
            num_seq = arguments["sequence"][0]
            contents = utils.get(LIST_SEQ, num_seq)
        elif path_name == "/gene":
            gene = arguments["gene"][0]
            contents = utils.gene(gene)
        elif path_name == "/operation":
            seq = arguments["seq"][0]
            op = arguments["op"][0]
            if op == "Info":
                contents = utils.info(seq)
            elif op == "Comp":
                contents = utils.comp(seq)
            elif op == "Rev":
                contents = utils.rev(seq)
        else:
            contents = utils.read_template(HTMLS + 'Error.html').render()

        self.send_response(200)  # -- Status line: OK!

        self.send_header('Content-Type', 'text/html')

        # noinspection PyTypeChecker
        self.send_header('Content-Length', len(str.encode(contents)))

        self.end_headers()

        self.wfile.write(str.encode(contents))

        return
Esempio n. 6
0
    def do_GET(self):

        termcolor.cprint(self.requestline, 'green')
        termcolor.cprint(self.path, 'blue')

        o = urlparse(self.path)
        path_name = o.path
        arguments = parse_qs(o.query)
        print("Resource requested: ", path_name)
        print("Parameters:", arguments)

        context = {}

        if path_name == "/":
            context['n_sequences'] = len(list_sequences)
            context['list_genes'] = list_genes
            contents = su.read_template_html_file('./html/index.html').render(context=context)
        elif path_name == '/test':
            contents = su.read_template_html_file('./html/test.html').render()
        elif path_name == '/ping':
            contents = su.read_template_html_file('./html/ping.html').render()
        elif path_name == '/get':
            number_sequence = arguments['sequence'][0]
            contents = su.get(list_sequences, number_sequence)
        elif path_name == '/gene':
            gene = arguments['gene'][0]
            contents = su.gene(gene)
        elif path_name == '/operation':
            sequence = arguments['sequence'][0]
            operation = arguments['calculation'][0]
            if operation == 'Info':
                contents = su.info(sequence)
            elif operation == 'Comp':
                contents = su.comp(sequence)
            elif operation == 'Rev':
                contents = su.rev(sequence)
        else:
            contents = su.read_template_html_file('./html/error.html').render()
        self.send_response(200)

        # Define the content-type header:
        self.send_header('Content-Type', 'text/html')
        self.send_header('Content-Length', len(contents.encode()))

        # The header is finished
        self.end_headers()

        # Send the response message
        self.wfile.write(contents.encode())

        return
Esempio n. 7
0
    def do_GET(self):
        print(self.requestline, "green")
        print((self.path, "blue"))

        o = urlparse(self.path)
        path_name = o.path
        arguments = parse_qs(o.query)
        print("Resource requested:", path_name)
        print("Parameters:", arguments)

        context = {}
        if path_name == "/":
            context["n_sequences"] = len(SEQUENCES_LISTS)
            context["list_genes"] = LIST_GENES
            contents = su.read_template_html_file("./html/index.html").render(
                context=context)
        elif path_name == "/ping":
            contents = su.read_template_html_file("./html/ping.html").render()
        elif path_name == "/get":
            number_sequence = arguments["sequence"][0]
            contents = su.get(SEQUENCES_LISTS, number_sequence)
        elif path_name == "/gene":
            gene = arguments["gene"][0]
            contents = su.gene(gene)
        elif path_name == "/operation":
            sequence = arguments["sequence"][0]
            operation = arguments["operation"][0]
            contents = su.operation(sequence, operation)

        else:
            contents = su.read_template_html_file("./html/error.html")

        self.send_response(200)

        self.send_header('Content-Type', 'text/html')

        self.send_header("Content-Length", str(len(contents.encode())))

        self.end_headers()
        self.wfile.write(bytes(contents, "utf-8"))
Esempio n. 8
0
    def do_GET(self):
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        # Print the request line
        termcolor.cprint(self.requestline, 'green')
        termcolor.cprint(self.path, 'blue')

        o = urlparse(self.path)
        path_name = o.path
        arguments = parse_qs(o.query)
        print("Resource requested: ", path_name)
        print("Parameters: ", arguments)


        # IN this simple server version:
        # We are NOT processing the client's request
        # It is a happy server: It always returns a message saying
        # that everything is ok

        context = {}
        if path_name == "/":
            context["n_sequences"] = len(list_sequences)  #We are passing both things in order to work w/ them in index.html
            context["list_genes"] = list_genes
            contents = su.read_template_html_file("./html/index.html").render(context=context)
        elif path_name == "/test":
            contents = su.read_template_html_file("./html/test.html").render()
        elif path_name == "/ping":
            contents = su.read_template_html_file("./html/ping.html").render()
        elif path_name == "/get":
            number_sequence = arguments["sequence"][0]
            contents = su.get(list_sequences, number_sequence)
        elif path_name == "/gene":
            gene = arguments["gene"][0]
            contents = su.gene(gene)
        elif path_name == "/operation":
            try:
                if arguments["calculation"][0] == "Info":
                    sequence = arguments["sequence"][0]
                    contents = su.info(sequence)
                elif arguments["calculation"][0] == "Rev":
                    sequence = arguments["sequence"][0]
                    contents = su.rev(sequence)
                elif arguments["calculation"][0] == "Comp":
                    sequence = arguments["sequence"][0]
                    contents = su.rev(sequence)
            except KeyError:
                contents = su.read_template_html_file("./html/button.html").render()

        else:
            contents = su.read_template_html_file("./html/error.html").render()


        # Generating the response message
        self.send_response(200)  # -- Status line: OK!

        # Define the content-type header:
        self.send_header('Content-Type', 'text/html') # Si no ponemos html saldrá texto!!
        self.send_header('Content-Length', len(contents.encode()))

        # The header is finished
        self.end_headers()

        # Send the response message
        self.wfile.write(contents.encode())

        return
    if len(formatted_msg) == 1:
        command = formatted_msg[0]
    else:
        command = formatted_msg[0] + '"'
        argument = '"' + formatted_msg[1]

    if command == '"PING"':
        server_utils.ping(cs)

    elif command == '"GET"':
        server_utils.get(list_sequences, cs, argument)

    elif command == '"INFO"':
        server_utils.info(argument, cs)

    elif command == '"COMP"':
        server_utils.comp(argument, cs)

    elif command == '"REV"':
        server_utils.rev(argument, cs)

    elif command == '"GENE"':
        server_utils.gene(argument, cs)

    else:
        response = "Not available command"
        termcolor.cprint(response, "red")
        cs.send(response.encode())
    cs.close()

#echo 2 | nc -w 1 127.0.0.1 8080
Esempio n. 10
0
    def do_GET(self):
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        # Print the request line
        termcolor.cprint(self.requestline, 'green')
        termcolor.cprint(self.path, 'blue')

        # IN this simple server version:
        # We are NOT processing the client's request
        # It is a happy server: It always returns a message saying
        # that everything is ok
        o = urlparse(self.path)
        path_name = o.path
        arguments = parse_qs(o.query)
        print(f"Resources requested: {path_name}")
        print(f"Parameters: {arguments}")
        # Message to send back to the client
        context = {}
        if path_name == "/":
            context["n_sequences"] = len(seq_list)
            context["gene_list"] = gene_list
            contents = read_template_html_file("./HTML_FILES/index.html").render(context=context)
        elif path_name == "/PING":
            contents = read_template_html_file("./HTML_FILES/ping.html").render()

        elif path_name == "/GET":
            number_sequence = arguments["sequence"][0]
            contents = su.get_sequence(number_sequence, seq_list)

        elif path_name == "/GENE":
            gene = arguments["gene"][0]
            contents = su.gene(gene)

        elif path_name == "/OPERATION":
            try:
                sequence = arguments["sequence"][0]
                operation = arguments["operation"][0]
                if operation == "Info":
                    contents = su.info(sequence)
                elif operation == "Comp":
                    contents = su.complementary(sequence)
                else:
                    contents = su.reverse(sequence)
            except KeyError:
                contents = read_html_file("./HTML_FILES/Error.html")

        else:
            contents = read_template_html_file("./HTML_FILES/Error.html").render()

        # Generating the response message
        self.send_response(200)  # -- Status line: OK!

        # Define the content-type header:
        self.send_header('Content-Type', 'text/html')
        self.send_header('Content-Length', len(contents.encode()))

        # The header is finished
        self.end_headers()

        # Send the response message
        self.wfile.write(contents.encode())

        return
Esempio n. 11
0
    def do_GET(self):
        global contents
        colorama.init(strip="False")
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        # Print the request line
        termcolor.cprint(self.requestline, 'green')
        termcolor.cprint(self.path, 'blue')

        o = urlparse(self.path)
        path_name = o.path
        arguments = parse_qs(o.query)
        print('Resource request: ', path_name)
        print('Parameters: ', arguments)

        # IN this simple server version:
        # We are NOT processing the client's request
        # It is a happy server: It always returns a message saying
        # that everything is ok
        # Message to send back to the client
        context = {}
        if path_name == "/":
            context['n_sequences'] = len(LIST_SEQUENCES)
            context['list_genes'] = LIST_GENES
            contents = su.read_template_html_file('./HTML/index.html').render(
                context=context)
        elif path_name == '/test':
            contents = su.read_template_html_file('./HTML/test.html').render()
        elif path_name == '/ping':
            contents = su.read_template_html_file('./HTML/ping.html').render()
        elif path_name == '/get':
            number_sequence = arguments['sequence'][0]
            contents = su.get(LIST_SEQUENCES, number_sequence)
        elif path_name == '/gene':
            gene = arguments['gene'][0]
            contents = su.gene(gene)
        elif path_name == "/operation":
            sequence = arguments["sequence"][0]
            operation = arguments["calculation"][0]
            if operation == "info":
                contents = su.info(sequence)
            elif operation == "comp":
                contents = su.comp(sequence)
            elif operation == "rev":
                contents = su.rev(sequence)
        else:
            contents = su.read_template_html_file('./HTML/error.html').render()

        # Generating the response message
        self.send_response(200)  # -- Status line: OK!
        #length = len(contents.encode())
        # Define the content-type header:
        self.send_header('Content-Type', 'text/HTML')
        self.send_header('Content-Length', str(len(contents.encode())))

        # The header is finished
        self.end_headers()

        # Send the response message
        self.wfile.write(contents.encode())

        return
Esempio n. 12
0
    except KeyboardInterrupt:
        print("Server stopped by the user")
        sl.close()
        exit()

    else:
        msg_bytes = client_socket.recv(2048)
        msg_string = msg_bytes.decode()
        useful_string = server_utils.modified_message(msg_string)
        separate_strings = useful_string.split(" ")

        if "PING" == server_utils.modified_message(msg_string):
            server_utils.ping(client_socket)

        elif separate_strings[0] == "GET":
            server_utils.get_sequence(client_socket, int(separate_strings[1]),
                                      seq_list)

        elif separate_strings[0] == "INFO":
            server_utils.info(client_socket, separate_strings[1])

        elif separate_strings[0] == "COMP":
            server_utils.complementary(client_socket, separate_strings[1])

        elif separate_strings[0] == "REV":
            server_utils.reverse(client_socket, separate_strings[1])

        elif separate_strings[0] == "GENE":
            server_utils.gene(client_socket, separate_strings[1])
        client_socket.close()
    if len(formatted_message) == 1:
        message = formatted_message[0]
    else:
        message = formatted_message[0]
        argument = formatted_message[1]

    if message == "PING":
        server_utils.ping(cs)
        print("OK!")

    elif message == "GET":
        n = int(formatted_message[1])
        server_utils.get(cs, n, SEQUENCES_LIST)

    elif message == "INFO":
        seq = formatted_message[1]
        server_utils.info(cs, seq)

    elif message == "COMP":
        seq = formatted_message[1]
        server_utils.comp(cs, seq)

    elif message == "REV":
        seq = formatted_message[1]
        server_utils.rev(cs, seq)

    elif message == "GENE":
        filename = f"{formatted_message[1]}.txt"
        server_utils.gene(cs, filename)
Esempio n. 14
0
    def do_GET(self):
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        # We just print a message
        print("GET received! Request line:")

        # Print the request line
        termcolor.cprint("  " + self.requestline, 'green')

        # Print the command received (should be GET)
        print("  Command: " + self.command)

        # Print the resource requested (the path)
        termcolor.cprint("  Path: " + self.path, "blue")

        # we are creating a parse object (easier way to work with the elements of the path
        o = urlparse(self.path)
        path_name = o.path
        arguments = parse_qs(o.query)
        print("Resource requested: ", path_name)
        print("Parameters: ", arguments)
        context = {}
        if self.path == "/":
            context['n_sequences'] = len(LIST_SEQUENCES)
            context['list_genes'] = LIST_GENES
            contents = su.read_template_html_file("./html/INDEX.html").render(context=context)
        elif path_name == "/test": # when working with forms my self.path always comes with a question mark at the end
            contents = su.read_template_html_file("Ctest.html").render()
        elif path_name =='/ping':
            contents = su.read_template_html_file("html/PING.html").render()
        elif path_name=='/get':
            number_sequence=arguments['sequence'][0]
            contents = su.get(LIST_SEQUENCES,number_sequence)
        elif path_name == '/operation':
            sequence = arguments['sequence'][0]
            operation = arguments['operation'][0]
            if operation=='Info':
                contents=su.info(sequence)
            elif operation=='Rev':
                contents = su.rev(sequence)
            elif operation == 'Comp':
                contents = su.comp(sequence)

        elif path_name =='/gene':
            gene = arguments['gene'][0]
            contents = su.gene(gene)
        else:
            contents = su.read_template_html_file("./html/ERROR.html").render()

        # Generating the response message
        self.send_response(200)  # -- Status line: OK!

        # Define the content-type header:
        self.send_header('Content-Type', 'text/html')
        self.send_header('Content-Length', len(contents.encode()))

        # The header is finished
        self.end_headers() # we always need to call the end headers method which forces to create an empty line of the HTTP message

        # Send the response message
        self.wfile.write(contents.encode()) # wfile acts like a socket, its just something that we can write on

        # IN this simple server version:
        # We are NOT processing the client's request
        return
Esempio n. 15
0
    if len(formatted_message) == 1:
        command = formatted_message[0]
    else:
        command = formatted_message[0]
        argument = formatted_message[1]

    if command == "PING":
        server_utils.ping(cs)

    elif command == "GET":
        server_utils.get(cs, list_sequences, argument)

    elif command == "INFO":
        server_utils.info(cs, argument)

    elif command == "COMP":
        server_utils.comp(cs, argument)

    elif command == "REV":
        server_utils.rev(cs, argument)

    elif command == "GENE":
        server_utils.gene(cs, argument, gene_list, GENE_FOLDER)

    else:
        response = "ERROR. Not Available Command"
        cs.send(str(response).encode())
        print(response)

    cs.close()  # -- Close the data socket
    def do_GET(self):
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        # Print the request line
        termcolor.cprint(self.requestline, 'green')
        termcolor.cprint(self.path, "blue")

        o = urlparse(self.path)
        path_name = o.path
        arguments = parse_qs(o.query)
        print("Resource requested: ", path_name)
        print("Parameters: ", arguments)

        context = {}
        # Message to send back to the client
        try:
            if path_name == "/":
                context["n_sequences"] = len(LIST_SEQUENCES)
                context["list_genes"] = LIST_GENES

                contents = su.read_template_html_file(
                    "./html/form-4.html").render(context=context)

            elif path_name == "/test":
                contents = su.read_template_html_file(
                    "./html/test.html").render()
            elif path_name == "/ping":
                contents = su.read_template_html_file(
                    "./html/ping.html").render()
            elif path_name == "/get":
                number_sequence = arguments["sequence"][0]
                contents = su.get(LIST_SEQUENCES, number_sequence)
            elif path_name == "/gene":
                gene = arguments["gene"][0]
                contents = su.gene(gene)
            elif path_name == "/operate":
                sequence = arguments["sequence"][0]
                operation = arguments["operation"][0]
                if operation == "Info":
                    result = su.info(sequence)
                    contents = su.read_template_html_file(
                        "./html/operation.html").render(sequence=sequence,
                                                        operation=operation,
                                                        result=result)

                elif operation == "Comp":
                    result = su.comp(sequence)
                    contents = su.read_template_html_file(
                        "./html/operation.html").render(sequence=sequence,
                                                        operation=operation,
                                                        result=result)
                elif operation == "Rev":
                    result = su.rev(sequence)
                    contents = su.read_template_html_file(
                        "./html/operation.html").render(sequence=sequence,
                                                        operation=operation,
                                                        result=result)
            else:
                contents = su.read_template_html_file(
                    "./html/Error.html").render(context="")
        except KeyError as e:
            # keyerror aparece cuando falta un argumento
            contents = su.read_template_html_file("./html/Error.html").render(
                context=f"keyerror{e}")
        # Generating the response message
        self.send_response(200)  # -- Status line: OK!

        # Define the content-type header:
        self.send_header('Content-Type', 'text/html')
        self.send_header('Content-Length', str(len(contents.encode())))

        # The header is finished
        self.end_headers()

        # Send the response message
        self.wfile.write(contents.encode())

        return
    formatted_message = formatted_message.split(" ")
    # print(formatted_message)

    if len(formatted_message) == 1:
        command = formatted_message[0]
    else:
        command = formatted_message[0]
        argument = formatted_message[1]

    if command == "PING":
        server_utils.ping(cs)
    elif command == "GET":
        print(msg, ":", server_utils.get(cs, list_sequences, argument))
    elif command == "INFO":
        print(server_utils.info(cs, argument))
    elif command == "COMP":
        print(server_utils.comp(cs, argument))
    elif command == "REV":
        print(server_utils.rev(cs, argument))
    elif command == "GENE":
        print(server_utils.gene(cs, argument))

    else:
        response = "not available command"
        cs.send(str(response).encode())
    cs.close()

# nc windows: echo "PING" | nc 127.0.0.1 8080
# nc linux: printf "PING" | nc 127.0.0.1 8080
Esempio n. 18
0
        print("Server stopped by the user")
        ls.close()
        exit()
    else:
        # print("A client has connected to the server!")
        msg = utils.format_command(cs.recv(2048).decode())
        if msg == "PING":
            res = "OK!"
            #            print(termcolor.colored("PING command!", "green"))
            utils.ping()
        elif "GET" in msg:
            res = list_seq[int(msg[-1])]
            utils.print_colored("GET", "green")
        elif "INFO" in msg:
            utils.print_colored("INFO", "green")
            res = utils.info(msg.replace("INFO ", ""))
        elif "COMP" in msg:
            utils.print_colored("COMP", "green")
            res = utils.comp(msg.replace("COMP ", ""))
        elif "REV" in msg:
            utils.print_colored("REV", "green")
            res = utils.rev(msg.replace("REV ", ""))
        elif "GENE" in msg:
            utils.print_colored("GENE", "green")
            res = utils.gene(msg.replace("GENE ", "../P0/Sequences/"))
        else:
            res = "... Not a valid command ..."
        print(res)
        cs.send(res.encode())
        cs.close()
Esempio n. 19
0
    def do_GET(self):  #Get all the requests
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        # Print the request line
        print(self.requestline)  #GET /info/A HTTP/1.1
        print(self.path)  #/info/A

        o = urlparse(self.path)  #Creating urlparse object --- #/test
        path_name = o.path  #get the path, es mas o menos parecido a self.path PERO NO es lo mismo
        arguments = parse_qs(
            o.query
        )  #todo_ aquello que esté despues de la interrogación del request del client
        #arguments va a ser un diccionario, que contenga cada argument como key, y como value su valor.

        #GET /echo?msg=ALELUYA&base=T HTTP/1.1  arguments == msg=ALELUYA&base=T HTTP/1.1
        #Argument 1 : msg = ALELUYA
        #Argument 2 : base = T

        print("Resource requested: ", path_name)
        print("Parameters:", arguments)

        # IN this simple server version, We are NOT processing the client's request

        #Contex is a dict that we create with the values we are going to pass to any file.html
        context = {}

        if path_name == '/':  #ESTA VA A SER NUESTRA PÁGINA PRINCIPAL, Y LO QUE SE VA A VER ESTÁ DENTRO DE INDEX.HTML
            context['n_sequences'] = len(
                LIST_SEQUENCES
            )  #creamos key y value para mandar esa informacion a INDEX-HTML, que la va a usar
            context[
                'list_genes'] = LIST_GENES  #creamos una nueva key y value, el valor sera una lista con los nombres de las secuencias de genes
            context['list_operations'] = LIST_OPERATIONS
            contents = su.read_template_html_file('./html/index.html').render(
                context=context)

        elif path_name == '/test':  #Cuando trabajamos con forms si usamos self.path, la ruta luego se le añade un ? al final. SOLO PASA CON FORMS

            #FORMS: For sending information from the client to the server we use the html forms
            #Lo mejor va a ser usar siempre PATH_NAME !! Para que nunca haya ningun problema

            contents = su.read_template_html_file('./html/test.html').render()

        elif path_name == '/ping':
            contents = su.read_template_html_file("./html/ping.html").render()

        elif path_name == '/get':
            """Cuando en el browser seleccionamos alguna de las secuencias:
            Resource requested:  /get
            Parameters: {'sequence': ['1']}"""

            number_sequence = arguments['sequence'][
                0]  #accedemos al valor de la key sequence.
            #Ese valor, es una lista, que a su vez, contiene sólo un elemento, que es el número
            #De la secuencia que queremos obtener. por tanto ponemos number_sequence = arguments['sequence'][0]
            """Una vez que el server tiene el número de la secuencia que el client quiere obtener,
            tenemos que hacer lo mismo que en la práctica 3 anterior, es decir:
            - """

            contents = su.get(
                LIST_SEQUENCES,
                number_sequence)  #esto es informacion que vamos a mandar
            #a la funcion get de server_utils (su). Esa informacion es el numero de la secuencia que queremos
            #y también, la lista de secuencias. Una vez que llegue a la función get, alli lo que va a hacer es,
            #Crear un diccionario recopilando toda la informacion, context, que será enviado a GET.HTML
            #para que se pueda imprimir la información deseada.
            """QUÉ ESTÁ OCURRIENDO EN SERVER_UTILS (SU):
            def get(list_sequences, seq_number):
            sequence = list_sequences[int(seq_number)] #almacenamos la secuencia que nos pide el client
            context = {'number': seq_number, 'sequence': list_sequences} #crear el HTML con la secuencia que nos ha pedido el client
            contents = read_template_html_file('./html.get.html').render(context=context)
            return contents """

        elif path_name == '/gene':
            gene = arguments['gene'][0]
            print('gene:', gene)
            contents = su.gene(gene)

        elif path_name == '/operation':

            try:
                """ Fijarnos en lo que nos sale en la terminal: 
                Resource requested:  /operation
                Parameters: {'sequence': ['AAAA'], 'calculation': ['Comp']}"""

                sequence = arguments['sequence'][0]
                operation = arguments['calculation'][0]

                if operation == 'Info':
                    contents = su.info(sequence, operation)
                elif operation == 'Comp':
                    contents = su.comp(sequence, operation)
                else:
                    contents = su.rev(sequence, operation)

            except KeyError:
                contents = su.read_template_html_file(
                    "./html/error.html").render()
        else:
            contents = su.read_template_html_file("./html/error.html").render()

        # Generating the response message
        self.send_response(200)  # -- Status line: OK!

        # Define the content-type header:
        self.send_header(
            'Content-Type',
            'text/html')  #ESPECIFICAR EL CONTENIDO QUE ESTAMOS MANDANDO
        self.send_header('Content-Length', len(contents.encode()))

        # The header is finished
        self.end_headers()

        # Send the response message
        self.wfile.write(contents.encode())

        return
Esempio n. 20
0
    def do_GET(self):
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        # Print the request line
        termcolor.cprint(self.requestline, 'green')
        termcolor.cprint(self.path, "blue")
        # IN this simple server version:
        # We are NOT processing the client's request
        # It is a happy server: It always returns a message saying
        # that everything is ok
        # Message to send back to the client
        o = urlparse(self.path)  # object
        path_name = o.path
        arguments = parse_qs(o.query)
        print('Resource requested: ', path_name)
        print('Parameters', arguments)

        file_name = self.path.strip("/")  # /info/A.html --> info/A.html
        context = {}
        if path_name == "/":
            context['n_sequences'] = len(list_sequences)
            context['list_genes'] = list_genes
            contents = server_utils.read_template_htm_file(
                './html/index.html').render(context=context)

        elif path_name == '/ping':
            contents = server_utils.read_template_htm_file(
                './html/ping.html').render()

        elif path_name == '/gene':
            gene = arguments['gene'][0]
            contents = server_utils.gene(gene)

        elif path_name == '/get':
            number_sequence = arguments['sequence'][0]
            contents = server_utils.get_seq(list_sequences, number_sequence)

        elif path_name.startswith('/operation'):
            if arguments == {} or len(arguments) == 1:
                contents = server_utils.read_template_htm_file(
                    './html/error2.html').render()
            else:
                sequence = arguments['sequence'][0]
                calculation = arguments['calculation'][0]

                if calculation == 'Info':
                    contents = server_utils.info_seq(sequence)

                elif calculation == 'Comp':
                    contents = server_utils.complement_seq(sequence)

                else:
                    contents = server_utils.reversed_seq(sequence)
        else:
            contents = server_utils.read_template_htm_file(
                './html/Error.html').render()

        # Generating the response message
        self.send_response(200)  # -- Status line: OK! # it means success

        # Define the content-type header:
        self.send_header('Content-Type', 'text/html')
        self.send_header('Content-Length', len(contents.encode()))

        # The header is finished  # despues de los send_headers, luego el end_headers
        self.end_headers()

        # Send the response message
        self.wfile.write(contents.encode())  # igual que el send

        return
Esempio n. 21
0
    formatted_message = formatted_message.split(" ")

    if len(formatted_message) == 1:
        command = formatted_message[0]
    else:
        command = formatted_message[0]
        argument = formatted_message[1]

    if formatted_message == "PING":
        server_utils.ping(cs)

    elif command == "GET":
        server_utils.get(cs, l_seqs, argument)

    elif command == "INFO":
        server_utils.info(cs, argument)

    elif command == "COMP":
        server_utils.comp(cs, argument)

    elif command == "REV":
        server_utils.rev(cs, argument)

    elif command == "GENE":
        server_utils.gene(cs, argument)

    else:
        response = "Not available command" + "\n"
        cs.send(str(response).encode())
    cs.close()
Esempio n. 22
0
    def do_GET(self):
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        # Print the request line
        termcolor.cprint(self.requestline, 'green')
        termcolor.cprint(self.path, 'blue')

        o = urlparse(self.path)
        path_name = o.path
        arguments = parse_qs(o.query)
        print("Resource requested: ", path_name)
        print("Parameters: ", arguments)
        # IN this simple server version:
        # We are NOT processing the client's request
        # It is a happy server: It always returns a message saying
        # that everything is ok
        context = {}
        # Message to send back to the client
        if path_name == '/':
            context["n_sequences"] = len(list_sequences)
            context["list_genes"] = list_genes
            contents = su.read_template_html_file('./html/index.html').render(
                context=context)
        elif path_name == "/test":  #needs a ? after test to work, we use the o=urlparse;; path_name;; arguments;; etc.
            contents = su.read_template_html_file("./html/test.html").render()
        elif path_name == "/ping":
            contents = su.read_template_html_file("./html/ping.html").render()
        elif path_name == "/get":
            number_sequence = arguments["sequence"][0]
            contents = su.get(list_sequences, number_sequence)
        elif path_name == "/gene":
            gene = arguments["gene"][0]
            contents = su.gene(gene)
        elif path_name == "/operation":
            sequence = arguments['sequence'][0]
            calculation = arguments['calculation'][0]
            if calculation == 'Info':
                contents = su.info(sequence)
            elif calculation == 'Comp':
                contents = su.comp(sequence)
            elif calculation == 'Rev':
                contents = su.rev(sequence)
            else:
                contents = su.read_template_html_file(
                    "./html/ERROR.html").render()
        else:
            contents = su.read_template_html_file("./html/ERROR.html").render()

        # Generating the response message
        self.send_response(200)  # -- Status line: OK!

        # Define the content-type header:
        self.send_header('Content-Type', 'text/html')
        self.send_header('Content-Length', len(contents.encode()))

        # The header is finished
        self.end_headers()

        # Send the response message
        self.wfile.write(contents.encode())

        return