2.2. A stupid one-purpose Web server

Exercise 2.5. A Web server that just returns a sequence

The following Web server just returns a FASTA formatted sequence, according to a given sequence identifier.

	      import os
import string

from BaseHTTPServer import HTTPServer
from BaseHTTPServer import BaseHTTPRequestHandler

class SeqHandler(BaseHTTPRequestHandler):
    
    def do_GET(self):
        seq_id = self.path
        result = self.get_seq(seq_id)
        if result is not None:
            self.printCustomHTTPResponse(200)
        else:
            self.printCustomHTTPResponse(404)
        self.wfile.write("<html>\n<body>\n")
        self.wfile.write("<p>Request: " + seq_id + "</p>\n" )
        if result is not None:
            self.wfile.write("<pre>\n" )
            self.wfile.write(result)
            self.wfile.write("</pre>\n" )
        self.wfile.write("\n</body>\n</html>\n")

    def printCustomHTTPResponse(self, respcode):
        self.send_response(respcode)
        self.send_header("Content-Type", "text/html")
        self.send_header("Server", "myHandler")
        self.end_headers()

    def get_seq(self, seq_id, db='sp'):
        os.environ['PATH'] = '/usr/local/gensoft/bin' + ':' + os.environ['PATH']
        cmd="golden " + db + ":" + seq_id
        handle = os.popen(cmd, 'r')
        entry = handle.readlines()
        status = handle.close()
        if status is not None:
            return None
        return string.join(entry, '')

if __name__ == "__main__":
    server = HTTPServer(('',2113), SeqHandler)
    for lp in range(5000):
        server.handle_request()


    
      
	  

How do you use this Web server?

How do you call the sequence identifier in Web parlance? Which standard part of the request does it represent?

What do you think about this server? What is generally the role of a Web server?