コード例 #1
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

        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
コード例 #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)
コード例 #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
        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 == "/":
            contents = su.read_template_html_file("./html/index.html").render(
                context=context)

        elif path_name == "/geneSeq":
            if 'gene_name' in arguments and not 'json' in arguments:
                contents = su.gene_sequence(arguments['gene_name'][0], True)
            elif 'gene_name' in arguments and 'json' in arguments:
                if arguments['json'][0] == '1':
                    contents = su.gene_sequence(arguments['gene_name'][0],
                                                False)
                else:
                    contents = {
                        'ERROR':
                        'json argument must be 1 to return json output'
                    }
            else:
                if not 'gene_name' in arguments and not 'json' in arguments:
                    contents = {'ERROR': 'You must introduce a gene name'}
                else:
                    contents = {
                        'ERROR':
                        'endpoint arguments are not correct for this endpoint'
                    }

        elif path_name == "/geneInfo":
            if 'gene_name' in arguments and not 'json' in arguments:
                contents = su.gene_info(arguments['gene_name'][0], True)
            elif 'gene_name' in arguments and 'json' in arguments:
                if arguments['json'][0] == '1':
                    contents = su.gene_info(arguments['gene_name'][0], False)
                else:
                    contents = {
                        'ERROR':
                        "json argument must be 1 to return json output"
                    }
            else:
                if not 'gene_name' in arguments and not 'json' in arguments:
                    contents = {'ERROR': 'You must introduce a gene name'}
                else:
                    contents = {
                        'ERROR':
                        'endpoint arguments are not correct for this endpoint'
                    }

        elif path_name == "/geneCalc":
            if 'gene_name' in arguments and not 'json' in arguments:
                contents = su.gene_calc(arguments['gene_name'][0], True)
            elif 'gene_name' in arguments and 'json' in arguments:
                if arguments['json'][0] == '1':
                    contents = su.gene_calc(arguments['gene_name'][0], False)
                else:
                    contents = {
                        'ERROR':
                        "json argument must be 1 to return json output"
                    }
            else:
                if not 'gene_name' in arguments and not 'json' in arguments:
                    contents = {'ERROR': 'You must introduce a gene name'}
                else:
                    contents = {
                        'ERROR':
                        'endpoint arguments are not correct for this endpoint'
                    }

        elif path_name == "/listSpecies":
            if 'limit' in arguments and not 'json' in arguments:
                contents = su.list_species(arguments['limit'][0], True)
            elif 'limit' in arguments and 'json' in arguments:
                if arguments['json'][0] == '1':
                    contents = su.list_species(arguments['limit'][0], False)
                else:
                    contents = {
                        'ERROR':
                        'json argument must be 1 to return json output'
                    }
            else:
                contents = {
                    'ERROR':
                    'endpoint arguments are not correct for this endpoint (Check you introduced a limit also!'
                }

        elif path_name == "/karyotype":
            if 'species' in arguments and not 'json' in arguments:
                contents = su.karyotype_by_specie(arguments['species'][0],
                                                  True)
            elif 'species' in arguments and 'json' in arguments:
                if arguments['json'][0] == '1':
                    contents = su.karyotype_by_specie(arguments['species'][0],
                                                      False)
                else:
                    contents = {
                        'ERROR':
                        'json argument must be 1 to return json output'
                    }
            else:
                contents = {
                    'ERROR':
                    'endpoint arguments are not correct for this endpoint. Check you have provided the name of a species'
                }

        elif path_name == "/chromosome_length":
            if 'species' in arguments and 'length' in arguments and not 'json' in arguments:
                contents = su.chromosome_length(arguments['species'][0],
                                                arguments['length'][0], True)
            elif 'species' in arguments and 'length' in arguments and 'json' in arguments:
                if arguments['json'][0] == '1':
                    contents = su.chromosome_length(arguments['species'][0],
                                                    arguments['length'][0],
                                                    False)
                else:

                    contents = {
                        'ERROR':
                        'json argument must be 1 to return json output'
                    }
            else:
                if 'species' in arguments and not 'length' in arguments and not 'json' in arguments:
                    contents = {
                        'ERROR':
                        'You missed the number of chromosome you want to check'
                    }
                elif 'length' in arguments and not 'species' in arguments and not 'json' in arguments:
                    contents = {
                        'ERROR':
                        'You missed the name of the species you want to check'
                    }
                elif not 'length' in arguments and not 'species' in arguments and not 'json' in arguments:
                    contents = {
                        'ERROR':
                        'You missed the name of the species and the name of the chromosome you want to check'
                    }
                else:
                    contents = {
                        'ERROR':
                        'endpoint arguments are not correct for this endpoint'
                    }

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

        # Generating the response message
        if type(contents) == str:
            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())
        elif type(contents) == dict:
            contents_str = json.dumps(contents)
            self.send_response(200)
            self.send_header('Content-Type', 'text/json')
            self.send_header('Content-Length', len(contents_str.encode()))
            # The header is finished
            self.end_headers()
            # Send the response message
            self.wfile.write(contents_str.encode())
        else:
            pass

        return