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
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())
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
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
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
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
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
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
def chromosmes(specie, number): SERVER = "rest.ensembl.org" ENDPOINT = "/info/assembly/" PARAMS = "?content-type=application/json" connection = http.client.HTTPConnection(SERVER) connection.request("GET", ENDPOINT + str(specie) + PARAMS) response = connection.getresponse() print(response.status, response.reason) answer_decoded = response.read().decode() dict_response = json.loads(answer_decoded) karyotipe = dict_response["top_level_region"] for n in range(0, len(karyotipe)): dictt_chromosomes = (karyotipe[n]) for key, values in dictt_chromosomes.items(): listt = [] if key == "name": listt.append(dictt_chromosomes) if values == number: length = dictt_chromosomes["length"] context = {"specie": specie, "length_chromosome": length, "number": number} content = su.read_template_html_file("./html/chromosome.html").render( context=context) return content
def species_name(limit_number): SERVER = "rest.ensembl.org" ENDPOINT = "/info/species/" PARAMS = "?content-type=application/json" connection = http.client.HTTPConnection(SERVER) connection.request("GET", ENDPOINT + PARAMS) response = connection.getresponse() print(response.status, response.reason) answer_decoded = response.read().decode() dict_response = json.loads(answer_decoded) species = (dict_response["species"]) total = len(species) llist = [] for n in range(0, limit_number): species_1 = species[n] name = species_1["display_name"] llist.append(name) names = llist context = { "number_of_species": limit_number, "species_name": names, "total_species": total } content = su.read_template_html_file("./html/list_species.html").render( context=context) return content
def infoSeq(gene, genes_dict): SERVER = "rest.ensembl.org" ENDPOINT = "/sequence/id/" PARAMS = "?content-type=application/json" ID = genes_dict[gene] connection = http.client.HTTPConnection(SERVER) connection.request("GET", ENDPOINT + ID + PARAMS) response = connection.getresponse() print(response.status, response.reason) answer_decoded = response.read().decode() dict_response = json.loads(answer_decoded) idd = dict_response["id"] desc = dict_response["desc"] chromosome_name = desc.split(":")[1] start = desc.split(":")[3] end = desc.split(":")[4] lenght = (int(end) - int(start)) context = { "gene": gene, "id": idd, "chromosome_name": chromosome_name, "start": start, "end": end, "lenght": lenght } content = su.read_template_html_file("./html/infoSeq.html").render( context=context) return content
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"))
def karyotype(specie): SERVER = "rest.ensembl.org" ENDPOINT = "/info/assembly/" PARAMS = "?content-type=application/json" connection = http.client.HTTPConnection(SERVER) connection.request("GET", ENDPOINT + str(specie) + PARAMS) response = connection.getresponse() print(response.status, response.reason) answer_decoded = response.read().decode() dict_response = json.loads(answer_decoded) karyotype = dict_response["karyotype"] context = {"species_karyotype": karyotype, "specie_name": specie} content = su.read_template_html_file("./html/karyotype.html").render( context=context) return content
def geneSeq(gene, genes_dict): SERVER = "rest.ensembl.org" ENDPOINT = "/sequence/id/" PARAMS = "?content-type=application/json" ID = genes_dict[gene] connection = http.client.HTTPConnection(SERVER) connection.request("GET", ENDPOINT + ID + PARAMS) response = connection.getresponse() print(response.status, response.reason) answer_decoded = response.read().decode() dict_response = json.loads(answer_decoded) sequence = dict_response["seq"] context = {"gene": gene, "sequence": sequence} content = su.read_template_html_file("./html/geneSeq.html").render( context=context) return content
def percentageSeq(gene, genes_dict): SERVER = "rest.ensembl.org" ENDPOINT = "/sequence/id/" PARAMS = "?content-type=application/json" ID = genes_dict[gene] connection = http.client.HTTPConnection(SERVER) connection.request("GET", ENDPOINT + ID + PARAMS) response = connection.getresponse() print(response.status, response.reason) answer_decoded = response.read().decode() dict_response = json.loads(answer_decoded) sequence = Seq(dict_response["seq"]) lenght = sequence.len() a, c, g, t = sequence.percentage(sequence.count_bases(), lenght) context = {"A": a, "C": c, "G": g, "T": t, "gene": gene} content = su.read_template_html_file("HTML/percentageCalc.html").render( context=context) return content
def do_GET(self): """This method is called whenever the client invokes the GET method in the HTTP protocol request""" 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 == "/": contents = server_utils.read_template_html_file( "./html/index.html").render(context=context) elif path_name == "/listSpecies": dict_s = server_utils.get_data("/info/species") if len(arguments) == 1: try: limit = arguments["limit"][0] except KeyError: limit = len(dict_s["species"]) contents = server_utils.list_species(dict_s, limit) else: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() elif path_name == "/karyotype": if len(arguments) == 1: try: specie_name = arguments["specie"][0] dict_k = server_utils.get_data("info/assembly" + "/" + specie_name) contents = server_utils.karyotype(dict_k, specie_name) except KeyError: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() except ValueError: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() else: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() elif path_name == "/chromosomeLength": if len(arguments) == 2: try: specie = arguments["specie"][0] chromo = arguments["chromo"][0] dict_c = server_utils.get_data("/info/assembly" + "/" + specie + "/" + chromo) contents = server_utils.length_chromosome( dict_c, specie, chromo) except KeyError: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() except ValueError: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() else: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() elif path_name == "/geneSeq": if len(arguments) == 1: try: gene = arguments["gene"][0] dict_seq = server_utils.get_data( "/sequence/id/" + server_utils.genes_dict[gene]) contents = server_utils.sequence(dict_seq, gene) except KeyError: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() except ValueError: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() else: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() elif path_name == "/geneInfo": if len(arguments) == 1: try: gene = arguments["gene"][0] dict_info = server_utils.get_data( "/sequence/id/" + server_utils.genes_dict[gene]) contents = server_utils.infoseq(dict_info, gene) except KeyError: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() except ValueError: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() else: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() elif path_name == "/geneCalc": if len(arguments) == 1: try: gene = arguments["gene"][0] dict_calc = server_utils.get_data( "/sequence/id/" + server_utils.genes_dict[gene]) contents = server_utils.calcseq(dict_calc, gene) except KeyError: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() except ValueError: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() else: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() else: contents = server_utils.read_template_html_file( "./html/ERROR.html").render() self.send_response(200) length = len(contents.encode()) # Define the content-type header: self.send_header('Content-Type', 'text/html') self.send_header('Content-Length', str(length)) # The header is finished self.end_headers() # Send the response message self.wfile.write(str.encode(contents)) return
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
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
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 = {} content_type = 'text/html' try: if path_name == '/': context['list'] = gene_dict.keys() contents = su.read_template_html_file( './html/index.html').render(context=context) elif path_name == '/listSpecies': ENDPOINT = '/info/species' l_species = [] try: limit = arguments['limit'][0] except ValueError: contents = su.read_template_html_file( './html/error.html').render(context=context) if limit.isdigit(): for specie in ( su.get_dict(ENDPOINT)['species'][0:int(limit)]): l_species.append(specie['name']) context = { "num_species": limit, "total_species": l_species, "length": len(su.get_dict(ENDPOINT)['species']) } contents = su.read_template_html_file( './html/species.html').render(context=context) else: contents = su.read_template_html_file( './html/error.html').render(context=context) # if 'json' in arguments: # contents = json.dumps(context) # content_type = "application/json" # else: # contents = su.read_template_html_file('./html/error.html').render(context=context) elif path_name == '/karyotype': ENDPOINT = "/info/assembly/" specie = arguments['specie'][0].replace(' ', '_') dict_response = su.get_dict(ENDPOINT + specie) k_list = [] for i in dict_response['karyotype']: k_list.append(i) context = {'karyotype': k_list} contents = su.read_template_html_file( './html/karyotype.html').render(context=context) # if 'json' in arguments: # contents = json.dumps(context) #content_type = "application/json" # else: # contents = su.read_template_html_file('./html/error.html').render(context=context) elif path_name == '/chromosome': ENDPOINT = "/info/assembly/" specie = arguments['specie'][0].replace(' ', '_') dict_response = su.get_dict(ENDPOINT + specie) chromosomes = arguments['chromosome'][0] for dictionary in dict_response['top_level_region']: if dictionary['name'] == chromosomes: context = { "chromosome": chromosomes, "length": dictionary['length'] } contents = su.read_template_html_file( './html/chromosome.html').render(context=context) break else: pass # if 'json' in arguments: # contents = json.dumps(context) # content_type = "application/json" # else: # contents = su.read_template_html_file('./html/error.html').render(context=context) elif path_name == "/geneSeq": ENDPOINT = "/sequence/id/" gene = arguments['gene'][0] ID = gene_dict[arguments['gene'][0]] dict_response = su.get_dict(ENDPOINT + ID) context = {'gene_name': gene, 'seq': dict_response['seq']} contents = su.read_template_html_file( './html/geneSeq.html').render(context=context) # if 'json' in arguments: # contents = json.dumps(context) # content_type = "application/json" # else: # contents = su.read_template_html_file('./html/error.html').render(context=context) elif path_name == "/geneInfo": ENDPOINT = "/sequence/id/" gene = arguments['gene'][0] ID = gene_dict[gene] dict_response = su.get_dict(ENDPOINT + ID) info = dict_response['desc'] length = str(int(info.split(":")[4]) - int(info.split(":")[3])) context = { 'gene_name': gene, 'length': length, 'start': info.split(":")[3], 'end': info.split(":")[4], 'name': info.split(":")[2] } contents = su.read_template_html_file( './html/geneInfo.html').render(context=context) # if 'json' in arguments: # contents = json.dumps(context) # content_type = "application/json" # else: # contents = su.read_template_html_file('./html/error.html').render(context=context) elif path_name == "/geneCalc": ENDPOINT = "/sequence/id/" gene = arguments['gene'][0] ID = gene_dict[gene] dict_response = su.get_dict(ENDPOINT + ID) sequence = Seq1.Seq(dict_response["seq"]) s_length = sequence.len() percentages = sequence.percentages(sequence.count_bases(), s_length) context = { "gene_name": gene, "length": s_length, "percentages": percentages.items() } contents = su.read_template_html_file( './html/geneCalc.html').render(context=context) # if 'json' in arguments: # contents = json.dumps(context) # content_type = "application/json" # else: # contents = su.read_template_html_file('./html/error.html').render(context=context) else: contents = su.read_template_html_file( "html/error.html").render() except (KeyError, IndexError, ValueError): contents = su.read_template_html_file('./html/error.html').render() self.send_response(200) self.send_header('Content-Type', content_type) self.send_header('Content-Length', len(contents.encode())) self.end_headers() self.wfile.write(contents.encode()) return
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
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) human_g_list = [] for key, ID in genes_dict.items(): gene = key human_g_list.append(gene) context = {} if path_name == "/": context = {"human_genes": human_g_list} contents = su.read_template_html_file("HTML/INDEX.html").render( context=context) elif path_name == "/listSpecies": try: limit_number = arguments["limit"][0] if 0 <= int(limit_number) <= 310: contents = t.species_name(int(limit_number)) elif int(limit_number) >= 310: contents = t.species_name(310) else: contents = su.read_template_html_file( "./HTML/ERROR.html").render() except ValueError: contents = su.read_template_html_file( "./HTML/ERROR.html").render() #try: #try: #try: #limit_number = arguments["limit"][0] #contents = t.species_name(int(limit_number)) #except KeyError: #contents = su.read_template_html_file("./HTML/ERROR.html").render() #except ValueError: #contents = su.read_template_html_file("./HTML/ERROR.html").render() #except IndexError: #contents = su.read_template_html_file("./HTML/ERROR.html").render() elif path_name == "/karyotype": try: specie = arguments["karyotype"][0] contents = t.karyotype(specie) print(type(specie)) except KeyError: contents = su.read_template_html_file( "./HTML/ERROR.html").render() elif path_name == "/chromosomeLength": try: try: specie = arguments["specie"][0] number = arguments["number"][0] contents = t.chromosmes(specie, number) except KeyError: contents = su.read_template_html_file( "./HTML/ERROR.html").render() except UnboundLocalError: contents = su.read_template_html_file( "./HTML/ERROR.html").render() elif path_name == "/geneSeq": gene = arguments["gene"][0] contents = t.geneSeq(gene, genes_dict) elif path_name == "/geneInfo": gene = arguments["gene"][0] contents = t.infoSeq(gene, genes_dict) elif path_name == "/geneCalc": gene = arguments["gene"][0] type_info = arguments["calculation"][0] if type_info == "length": contents = t.lengthSeq(gene, genes_dict) elif type_info == "percentage": contents = t.percentageSeq(gene, genes_dict) # 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
def do_GET(self): """This method is called whenever the client invokes the GET method in the HTTP protocol request""" connection = http.client.HTTPConnection(SERVER) # 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 = {} try: if path_name == "/": contents = su.read_template_html_file( "./html/INDEX.html").render(context=context) elif path_name == "/listSpecies": ENDPOINT = "/info/species" connection.request("GET", ENDPOINT + Parameters) response = connection.getresponse() response_dict = json.loads(response.read().decode()) species_list = [] amount_species = len(response_dict["species"]) context["amount_species"] = amount_species limit = int(arguments["limit"][0]) context["limit"] = limit for n in range(0, limit): species_list.append( response_dict["species"][n]["common_name"]) context["names"] = species_list contents = su.read_template_html_file( "html/LISTSPECIES.html").render(context=context) elif path_name == "/karyotype": ENDPOINT = "info/assembly/" specie = arguments["species"][0] connection.request("GET", ENDPOINT + specie + Parameters) response = connection.getresponse() response_dict = json.loads(response.read().decode()) karyotype = response_dict["karyotype"] context["species"] = arguments["species"][0] context["karyotype"] = karyotype contents = su.read_template_html_file( "./html/KARYOTYPE.html").render(context=context) elif path_name == "/chromosomeLength": ENDPOINT = "info/assembly/" specie = arguments["species"][0] connection.request("GET", ENDPOINT + specie + Parameters) response = connection.getresponse() response_dict = json.loads(response.read().decode()) chromosome = arguments["chromosome"][0] for n in range(0, len(response_dict["top_level_region"])): if chromosome == response_dict["top_level_region"][n][ "name"]: length = response_dict["top_level_region"][n]["length"] context["length"] = length contents = su.read_template_html_file( "./html/CHROMOSOMELENGTH.html").render(context=context) elif path_name == "/geneSeq": ENDPOINT = "/sequence/id/" gene = arguments["gene"][0] id = DICT_GENES[gene] connection.request("GET", ENDPOINT + id + Parameters) response = connection.getresponse() response_dict = json.loads(response.read().decode()) context["seq"] = response_dict["seq"] contents = su.read_template_html_file( "./html/SEQGENE.html").render(context=context) elif path_name == "/geneInfo": ENDPOINT = "/sequence/id/" gene = arguments["gene"][0] id = DICT_GENES[gene] connection.request("GET", ENDPOINT + id + Parameters) response = connection.getresponse() response_dict = json.loads(response.read().decode()) info = response_dict["desc"].split(":") context["dict_info"] = { "Name": info[1], "ID": id, "Start": info[3], "End": info[4], "Length": (int(info[4]) - int(info[3]) + 1) } contents = su.read_template_html_file( "./html/INFOGENE.html").render(context=context) elif path_name == "/geneCalc": ENDPOINT = "/sequence/id/" gene = arguments["gene"][0] id = DICT_GENES[gene] connection.request("GET", ENDPOINT + id + Parameters) response = connection.getresponse() response_dict = json.loads(response.read().decode()) sequence = Seq(response_dict["seq"]) dict_bases = Seq.count(sequence) percentage = Seq.percentage(sequence) context["length"] = Seq.len(sequence) context["bases"] = { "A": str(dict_bases["A"]) + " (" + str(percentage[0]) + "%)", "C": str(dict_bases["C"]) + " (" + str(percentage[1]) + "%)", "T": str(dict_bases["T"]) + " (" + str(percentage[2]) + "%)", "G": str(dict_bases["G"]) + " (" + str(percentage[3]) + "%)" } contents = su.read_template_html_file( "./html/CALCGENE.html").render(context=context) else: contents = su.read_template_html_file( "./html/ERROR.html").render() except KeyError: contents = su.read_template_html_file("./html/ERROR.html").render() except IndexError: 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
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
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
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
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
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
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 # Message to send back to the client context = {} response_dict = su.get_dict(ENDPOINT1) # putting all of the species into one list species_full = response_dict['species'] species_name = [] if path_name == '/': context = {} if 'json' in arguments: if arguments['json'][0] == '1': contents = json.dumps(context) content_type = 'application/json' error_code = 200 else: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 else: contents = su.read_template_html_file( './html/index.html').render() content_type = 'text/html' error_code = 200 elif path_name == '/listSpecies': try: for e in species_full: species_name.append(e['display_name']) if 'limit' not in arguments: context['length'] = len(species_name) context['list_species'] = species_name if 'json' in arguments: if arguments['json'][0] == '1': contents = json.dumps(context) content_type = 'application/json' error_code = 200 else: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 else: contents = su.read_template_html_file( './html/list_lim_no.html').render(context=context) content_type = 'text/html' error_code = 200 else: if int(arguments['limit'][0]) <= len(species_name): context['length'] = len(species_name) context['list_species'] = species_name context['limit'] = int(arguments['limit'][0]) if 'json' in arguments: if arguments['json'][0] == '1': context['list_species'] = [] for e in range(0, int(arguments['limit'][0])): context['list_species'].append( species_name[e]) contents = json.dumps(context) content_type = 'application/json' error_code = 200 else: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 else: contents = su.read_template_html_file( './html/list_species.html').render( context=context) content_type = 'text/html' error_code = 200 else: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 except KeyError: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 elif path_name == '/karyotype': try: if 'json' not in arguments: species_input = arguments['specie'][0] response_dict = su.get_dict(ENDPOINT2 + species_input) karyotype_list = response_dict['karyotype'] context = {'karyotype_list': karyotype_list} contents = su.read_template_html_file( './html/karyotype.html').render(context=context) content_type = 'text/html' error_code = 200 else: if arguments['json'][0] == '1': species_input = arguments['specie'][0] response_dict = su.get_dict(ENDPOINT2 + species_input) karyotype_list = response_dict['karyotype'] context = {'karyotype_list': karyotype_list} contents = json.dumps(context) content_type = 'application/json' error_code = 200 else: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 except KeyError: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 elif path_name == '/chromosomeLength': try: if 'json' in arguments: if arguments['json'][0] == '1': if 'specie' not in arguments: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 elif 'chromo' not in arguments: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 else: species_input = arguments['specie'][0] chromo_input = arguments['chromo'][0] response_dict = su.get_dict(ENDPOINT2 + species_input) for e in response_dict['top_level_region']: if e['name'] == chromo_input and e[ 'coord_system'] == 'chromosome': length_chromo = e['length'] else: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 context = {'length_chromo': length_chromo} contents = json.dumps(context) content_type = 'application/json' error_code = 200 else: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 else: if 'specie' not in arguments: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 elif 'chromo' not in arguments: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 else: species_input = arguments['specie'][0] chromo_input = arguments['chromo'][0] response_dict = su.get_dict(ENDPOINT2 + species_input) for e in response_dict['top_level_region']: if e['name'] == chromo_input and e[ 'coord_system'] == 'chromosome': length_chromo = e['length'] else: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 context = {'length_chromo': length_chromo} contents = su.read_template_html_file( './html/length.html').render(context=context) content_type = 'text/html' error_code = 200 except KeyError: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 elif path_name == '/geneSeq': try: if 'json' in arguments: if arguments['json'][0] == '1': gene_ask = arguments['gene'][0] id_gene = DICT_GENES_ID[gene_ask] response_dict = su.get_dict(ENDPOINT3 + id_gene) sequence_asked = response_dict['seq'] context = { 'sequence': sequence_asked, 'gene': gene_ask } contents = json.dumps(context) content_type = 'application/json' error_code = 200 else: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 else: gene_ask = arguments['gene'][0] id_gene = DICT_GENES_ID[gene_ask] response_dict = su.get_dict(ENDPOINT3 + id_gene) sequence_asked = response_dict['seq'] context = {'sequence': sequence_asked, 'gene': gene_ask} contents = su.read_template_html_file( './html/geneSeq.html').render(context=context) content_type = 'text/html' error_code = 200 except KeyError: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 elif path_name == '/geneInfo': try: gene_ask = arguments['gene'][0] id_gene = DICT_GENES_ID[gene_ask] response_dict = su.get_dict(ENDPOINT3 + id_gene) chromosome_info_list = response_dict['desc'].split(':') name_chromosome = chromosome_info_list[1] number_chromosome = chromosome_info_list[2] start_coordinates = chromosome_info_list[3] end_coordinates = chromosome_info_list[4] lists_info = su.get_list_info(response_dict) context = { 'name_chromosome': name_chromosome, 'number_chromosome': number_chromosome, 'start_coordinates': start_coordinates, 'end_coordinates': end_coordinates, 'id': id_gene, 'length_gene': lists_info[4], 'gene': gene_ask } if 'json' in arguments: if arguments['json'][0] == '1': contents = json.dumps(context) content_type = 'application/json' error_code = 200 else: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 else: contents = su.read_template_html_file( './html/geneInfo.html').render(context=context) content_type = 'text/html' error_code = 200 except KeyError: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 elif path_name == '/geneCalc': try: gene_ask = arguments['gene'][0] id_gene = DICT_GENES_ID[gene_ask] response_dict = su.get_dict(ENDPOINT3 + id_gene) lists_info = su.get_list_info(response_dict) context = { 'gene': gene_ask, 'length': lists_info[4], 'A': lists_info[0], 'G': lists_info[2], 'C': lists_info[1], 'T': lists_info[3] } if 'json' in arguments: if arguments['json'][0] == '1': contents = json.dumps(context) content_type = 'application/json' error_code = 200 else: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 else: contents = su.read_template_html_file( './html/geneCalc.html').render(context=context) content_type = 'text/html' error_code = 200 except KeyError: contents = su.read_template_html_file( './html/ERROR.html').render() content_type = 'text/html' error_code = 404 else: contents = su.read_template_html_file('./html/ERROR.html').render() content_type = 'text/html' error_code = 404 # Generating the response message self.send_response(error_code) # -- Status line: OK! # Define the content-type header: self.send_header('Content-Type', content_type) 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