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())
示例#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("PATH NAME: ", path_name)
        print("Arguments: ", 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),
                "gene_list": GENE_LIST
            }
            contents = su.read_template_html_file(ROOT +
                                                  "/Main_form.html").render(
                                                      context=context)
        elif path_name == "/ping":
            contents = su.read_template_html_file(ROOT +
                                                  "/ping_ok.html").render()
        elif path_name == "/get":
            contents = su.get(LIST_SEQUENCES, int(arguments["sequence"][0]))
        elif path_name == "/gen":
            contents = su.gen(arguments["gene"][0])
        elif path_name == "/operations":
            sequence = arguments["sequence"][0]
            calculation = arguments["calculations"][0]
            contents = su.operations(sequence, calculation)

        else:
            contents = su.read_template_html_file(ROOT +
                                                  "/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
示例#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)
        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
示例#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
        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
示例#5
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
示例#6
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
示例#7
0
    def do_GET(self):

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

        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_seq"] = 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_seq = arguments["sequence"][0]
            contents = su.get(LIST_SEQUENCES, number_seq)
        elif path_name == "/gene":
            gene = arguments["gene"][0]
            contents = su.genes(gene)
        elif path_name =="/operation":
            sequence = arguments["sequence"][0]
            operation = arguments["calculation"][0]
            if operation =="Rev":
                contents = su.reverse(sequence)
            elif operation == "Info":
                contents = su.bases(sequence)
            elif operation == "Comp":
                contents = su.complement(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', len(contents.encode()))

        # The header is finished
        self.end_headers()

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

        return
示例#8
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
示例#9
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"))
示例#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')

        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
        formatted_message = server_utils.format_command(msg)
        formatted_message = formatted_message.split(" ")
        if len(formatted_message) == 1:
            command = formatted_message[0]
        else:
            command =formatted_message[0]
            argument = formatted_message[1]

        if command == "PING":
            response = 'OK!\n'
            server_utils.ping()
            # -- The message has to be encoded into bytes
            cs.send(response.encode())
        if command == 'GET':
            if argument == '0':
                server_utils.get(cs, list_sequences, 0)
            elif argument == '1':
                server_utils.get(cs, list_sequences, 1)
            elif argument == '2':
                server_utils.get(cs, list_sequences, 2)
            elif argument == '3':
                server_utils.get(cs, list_sequences, 3)
            elif argument == '4':
                server_utils.get(cs, list_sequences, 4)
        if command == 'INFO':
            server_utils.info_send(cs, argument)
        if command == 'COMP':
            server_utils.comp_send(cs, argument)
        if command == 'REV':
            server_utils.rev_send(cs, argument)
        if command == 'GENE':
示例#12
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
    formatted_message = server_utils.format_command(msg)
    formatted_message = formatted_message.split(" ")

    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"
示例#14
0
    formatted_message = server_utils.format_command(msg)
    print(formatted_message)
    formatted_message = formatted_message.split(" ")

    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":
        number_sequence = argument["sequence"][0]
        server_utils.get(cs, list_sequences, number_sequence)

    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"
示例#15
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
示例#16
0
    formatted_message = server_utils.format_command(msg)
    print(formatted_message)
    formatted_message = formatted_message.split(" ")

    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"
    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
    msg = msg_raw.decode()
    formatted_message = server_utils.format_command(msg).replace('"', '')

    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
示例#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
示例#20
0
    formatted_message = server_utils.format_command(msg)
    print(formatted_message)
    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"
示例#21
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