2.3. CGI scripts that do something useful

Exercise 2.6. A CGI to fetch a sequence with the golden program

Write a CGI script called fetch_seq.py that fetches a sequence given its sequence identifier provided in the query part of the url, e.g: http://leeloo.sis.pasteur.fr/cgi-bin/IEB/fetch_seq.py?seq_id=MALK_ECOLI. This script should return the entry as plain text, without any HTML formatting.

For this purpose, you might need to write a get_seq function in your sequences module, such as:

import commands

def get_seq (id, db='sp'):
    cmd="golden " + db + ":" + id
    status, output = commands.getstatusoutput(cmd)
    if status != 0:
        return status, "A problem occured: " + output + '\nstatus: ' + str(status) + '\n'
    return None, output
You can use this function from within another get_seq function in your CGI:
def get_seq (id, db='sp'):
    os.environ['PATH'] = '/usr/local/bin:' + os.environ['PATH'] 
    os.environ['GOLDENDATA'] = '/local/cours/share/golden'
    status, output = sequences.get_seq(id=id, db=db)
    if status is None:
        return output
    return "A problem occured: " + output + '\nstatus: ' + str(status) + '\n'
Also check that the PATH to find the golden command: it is not necessarily present in the Web server PATH (see Exercise 2.4).

To let you handle correctly the query string, the cgi module provides a parse function that returns a dictionary. Each entry of this dictionary contains a list of values associated with a given field. So, given the above url, the dictionary will have an entry for 'seq_id', containing:

When would you have more than one value in this list? Guess the url that would leads to:

Write the form to invoke this CGI and call it: fetch_seq_post.html.

Exercise 2.7. Testing input

Write the control_ok function, a function that controls an input field. An input field should contain only letters, digits, and the following characters: , - _. The dot (.) is authorized, provided there is only one (e.g: .. is not allowed).

seq_id = query['seq_id'][0]
if control_ok(seq_id):
    print get_seq(seq_id)
    print "Your input is not valid. Please provide a correct entry_name"