python - Better way of checking HTTP file extensions requested by clients than if blocks -


i writing http server in python 2.7.8 , check file extension requested before sending respond in if,else blocks.

is there better way in python?

here code:

from basehttpserver import basehttprequesthandler,httpserver   #!/usr/bin/python basehttpserver import basehttprequesthandler,httpserver os import curdir, sep import cgi  port_number = 8080    class myhandler(basehttprequesthandler):      #handler requests     def do_get(self):         if self.path=="/":             self.path="/index_example3.html"          try:             #check file extension required ,             #set right mime type              sendreply=false             if self.path.endswith(".html"):                 mimetype='text/html'                 sendreply=true             if self.path.endswith(".jpg"):                 mimetype='image/jpg'                 sendreply=true             if self.path.endswith(".gif"):                 mimetype='image/gif'                 sendreply=true             if self.path.endswith(".js"):                 mimetype='application/javascript'                 sendreply=true             if self.path.endswith(".css"):                 mimetype='text/css'                 sendreply=true              if sendreply == true:                 #open static file requested , send                 f=open(curdir+sep+self.path)                 self.send_response(200)                 self.send_header('content-type',mimetype)                 self.wfile.write(f.read())                 f.close()             return         except ioerror:             self.send_error(404,'file not found'% self.path)       #handler post requests     def do_post(self):        if self.path=="/send":             form = cgi.fieldstorage(                 fp=self.rfile,                 headers=self.headers,                 environ={'request_method':'post',                          'content_type':self.headers['content-type']                })              print "your name is: %s" % form["your_name"].value             self.send_response(200)             self.end_headers()             self.wfile.write("thanks %s !" % form["your_name"].value)             return  try:     #create web server , define thehandler manage     #incoming request     server = httpserver(('',port_number),myhandler)     print('started httpserver on port ',port_number)     #wait forever incoming http requests     server.serve_forever()  except keyboardinterrupt:     print '^c received, shuting down web server'     server.socket.close() 

rather use large number of if tests, split off extension , use dictionary lookup:

import os.path  extension = os.path.splitext(self.path)[-1] mime_types = {     'html': 'text/html',      'jpg':'image/jpg',     'gif': 'image/gif',     'js': 'application/javascript',     '.css': 'text/css', } mimetype = mime_types.get(extension) sendreply = mimetype not none 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -