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["list_genes"] = list(HUMAN_GENES.keys())
            contents = su.read_template_html_file("./html/index.html").render(
                context=context)
        elif path_name == "/listSpecies":
            contents = su.listSpecies(arguments)
            if "check" in arguments.keys():
                print('The check bottom was used')
            else:
                print('The checkbox was not used')

        elif path_name == "/karyotype":
            contents = su.karyotype(arguments, path_name)
        elif path_name == "/chromosomeLength":
            contents = su.karyotype(arguments, path_name)
        elif path_name == "/geneSeq":
            contents = su.gene_seq(arguments, path_name, HUMAN_GENES)
        elif path_name == "/geneInfo":
            contents = su.gene_seq(arguments, path_name, HUMAN_GENES)
        elif path_name == "/geneCalc":
            contents = su.gene_seq(arguments, path_name, HUMAN_GENES)

        else:
            contents = su.read_template_html_file(
                "html/errors/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
Exemple #2
0
    def do_GET(self):
        termcolor.cprint(self.requestline, 'green')
        termcolor.cprint(self.path, "blue")

        url = urlparse(self.path)
        endpoint = url.path
        parameters = parse_qs(url.query)
        print("Endpoint: ", endpoint)
        print("Parameters: ", parameters)

        has_error = False
        contents = ""
        status = 400  # BAD_REQUEST
        if endpoint in ENDPOINTS:
            if endpoint == "/":
                status = 200
                contents = Path("./html/index.html").read_text()
            elif endpoint == "/listSpecies":
                if len(parameters) == 0:
                    status, contents = su.list_species()
                elif len(parameters) == 1:
                    try:
                        limit = int(parameters['limit'][0])
                        status, contents = su.list_species(limit)
                    except (KeyError, ValueError):
                        has_error = True
                else:
                    has_error = True
            elif endpoint == "/karyotype":
                if len(parameters) == 1:
                    try:
                        specie = parameters['specie'][0]
                        status, contents = su.karyotype(specie)
                    except (KeyError, ValueError):
                        has_error = True
                else:
                    has_error = True
            elif endpoint == "/chromosomeLength":
                if len(parameters) == 2:
                    try:
                        specie = parameters['specie'][0]
                        chromo = parameters['chromo'][0]
                        status, contents = su.chromosome_length(specie, chromo)
                    except (KeyError, ValueError):
                        has_error = True
                else:
                    has_error = True
            elif endpoint == "/geneSeq":
                if len(parameters) == 1:
                    try:
                        gene = parameters['gene'][0]
                        status, contents = su.gene_seq(gene)
                    except (KeyError, ValueError):
                        has_error = True
                else:
                    has_error = True
            elif endpoint == "/geneInfo":
                if len(parameters) == 1:
                    try:
                        gene = parameters['gene'][0]
                        status, contents = su.gene_info(gene)
                    except (KeyError, ValueError):
                        has_error = True
                else:
                    has_error = True
            elif endpoint == "/geneCalc":
                if len(parameters) == 1:
                    try:
                        gene = parameters['gene'][0]
                        status, contents = su.gene_calc(gene)
                    except (KeyError, ValueError):
                        has_error = True
                else:
                    has_error = True
        else:
            has_error = True

        if has_error:
            contents = Path("./html/error.html").read_text()

        self.send_response(status)
        self.send_header('Content-Type', 'text/html')
        self.send_header('Content-Length', str(len(contents.encode())))
        self.end_headers()
        self.wfile.write(contents.encode())
        print(contents)
    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

        try:
            if path_name == "/":
                contents = SU.read_template_html_file(
                    "./html/index.html").render()
                content_type = "text/html"

            elif path_name == "/listSpecies":
                if "limit" in arguments.keys() and len(arguments) == 1:
                    limit_species = arguments["limit"][0]
                    if limit_species.isdigit() and int(limit_species) > 0:
                        contents = SU.list_species(limit_species,
                                                   json_param=False)
                    else:
                        contents = SU.read_template_html_file(
                            "./html/Error.html").render()
                    content_type = "text/html"
                elif "limit" and "json" in arguments.keys(
                ) and arguments["json"][0] == "1" and len(arguments) == 2:
                    limit_species = arguments["limit"][0]
                    contents = SU.list_species(limit_species, json_param=True)
                    contents = json.dumps(contents)
                    content_type = "application/json"
                else:
                    contents = SU.read_template_html_file(
                        "./html/Error.html").render()
                    content_type = "text/html"
                print(contents)

            elif path_name == "/karyotype":
                if "specie" in arguments.keys() and len(arguments) == 1:
                    specie = arguments["specie"][0]
                    contents = SU.information_karyotype(specie,
                                                        json_param=False)
                    content_type = "text/html"
                elif "specie" and "json" in arguments.keys(
                ) and arguments["json"][0] == "1" and len(arguments) == 2:
                    specie = arguments["specie"][0]
                    contents = SU.information_karyotype(specie,
                                                        json_param=True)
                    contents = json.dumps(contents)
                    content_type = "application/json"
                else:
                    contents = SU.read_template_html_file(
                        "./html/Error.html").render()
                    content_type = "text/html"
                print(contents)

            elif path_name == "/chromosomeLength":
                if "specie" and "chromo" in arguments.keys() and len(
                        arguments) == 2:
                    specie = arguments["specie"][0]
                    chromosome = arguments["chromo"][0]
                    contents = SU.chromosome_length(specie,
                                                    chromosome,
                                                    json_param=False)
                    content_type = "text/html"
                elif "specie" and "chromo" and "json" in arguments.keys() and arguments["json"][0] == "1" \
                        and len(arguments) == 3:
                    specie = arguments["specie"][0]
                    chromosome = arguments["chromo"][0]
                    contents = SU.chromosome_length(specie,
                                                    chromosome,
                                                    json_param=True)
                    contents = json.dumps(contents)
                    content_type = "application/json"
                else:
                    contents = SU.read_template_html_file(
                        "./html/Error.html").render()
                    content_type = "text/html"
                print(contents)

            elif path_name == "/geneSeq":
                if "gene" in arguments.keys() and len(arguments) == 1:
                    gene = arguments["gene"][0]
                    contents = SU.gene_seq(gene, json_param=False)
                    content_type = "text/html"
                elif "gene" and "json" in arguments.keys(
                ) and arguments["json"][0] == "1" and len(arguments) == 2:
                    gene = arguments["gene"][0]
                    contents = SU.gene_seq(gene, json_param=True)
                    contents = json.dumps(contents)
                    content_type = "application/json"
                else:
                    contents = SU.read_template_html_file(
                        "./html/Error.html").render()
                    content_type = "text/html"
                print(contents)

            elif path_name == "/geneInfo":
                if "gene" in arguments.keys() and len(arguments) == 1:
                    gene = arguments["gene"][0]
                    contents = SU.gene_info(gene, json_param=False)
                    content_type = "text/html"
                elif "gene" and "json" in arguments.keys(
                ) and arguments["json"][0] == "1" and len(arguments) == 2:
                    gene = arguments["gene"][0]
                    contents = SU.gene_info(gene, json_param=True)
                    contents = json.dumps(contents)
                    content_type = "application/json"
                else:
                    contents = SU.read_template_html_file(
                        "./html/Error.html").render()
                    content_type = "text/html"
                print(contents)

            elif path_name == "/geneCalc":
                if "gene" in arguments.keys() and len(arguments) == 1:
                    gene = arguments["gene"][0]
                    contents = SU.gene_calc(gene, json_param=False)
                    content_type = "text/html"
                elif "gene" and "json" in arguments.keys(
                ) and arguments["json"][0] == "1" and len(arguments) == 2:
                    gene = arguments["gene"][0]
                    contents = SU.gene_calc(gene, json_param=True)
                    contents = json.dumps(contents)
                    content_type = "application/json"
                else:
                    contents = SU.read_template_html_file(
                        "./html/Error.html").render()
                    content_type = "text/html"
                print(contents)

            else:
                contents = SU.read_template_html_file(
                    "./html/Error.html").render()
                content_type = "text/html"

        except requests.exceptions.HTTPError:
            contents = SU.read_template_html_file("./html/Error.html").render()
            print(contents)
            content_type = "text/html"
        except KeyError:
            contents = SU.read_template_html_file("./html/Error.html").render()
            print(contents)
            content_type = "text/html"

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

        # Define the content-type header:
        self.send_header('Content-Type', content_type)
        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