title: How do I get WSGI.py to serve images in static html fies?
body: <p>Managing a Python (wsgi)-based site for a nonprofit &amp; for historical reasons want to serve old (static) HTML from one subdomain.</p>
  
  <p>Here is wsgi.py:</p>
  
  <p>(Note that html/css files are in static/ folder, others in a subfolder of static/)
  (This serves all the text w/ styles applied, but gives the alt text for images)</p>
  
  <pre><code>def application(env, start_response):
  # some debugging queries to check what's getting passed in:
  if env["QUERY_STRING"] == "env":
          start_response('200 OK', [('Content-Type',
                                   'text/html; charset=utf-8')])
          return ['&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;meta charset="utf-8"&gt;',
                  '&lt;title&gt;Environment&lt;/title&gt;',
                  repr(env),
                 "&lt;/html&gt;"]
      if env["QUERY_STRING"] == "req":
          start_response('200 OK', [('Content-Type',
                                   'text/html; charset=utf-8')])
      return ['&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;meta charset="utf-8"&gt;',
              '&lt;title&gt;Incoming request dump&lt;/title&gt;',
              "Scriptname: " + env["SCRIPT_NAME"] + "&lt;br&gt;",
              "Pathinfo: " + env["PATH_INFO"] + "&lt;br&gt;",
              "Querystring: " + env["QUERY_STRING"] + "&lt;br&gt;",
             "&lt;/html&gt;"]
  
  # the regular server code (runs OK):
      try:
          if env["PATH_INFO"] == "/": #root, no trailing '/'
              htmlfile = open("static/index.html")
              start_response('200 OK', [('Content-Type',
                                   'text/html; charset=utf-8')])
          return [htmlfile.read()]
      elif env["PATH_INFO"].endswith(".html"):
          htmlfile = open("static" + env["PATH_INFO"]) #
          start_response('200 OK', [('Content-Type',
                                   'text/html; charset=utf-8')])
          return [htmlfile.read()]
      elif env["PATH_INFO"].endswith(".css"):
          cssfile = open("static" + env["PATH_INFO"]) #
          start_response('200 OK', [('Content-Type',
                                   'text/css; charset=utf-8')])
          return [cssfile.read()]
  
  # This generates "Internal server error:"
      elif env["PATH_INFO"].endswith(".jpg") or
           env["PATH_INFO"].endswith(".JPG"):
          jpegfile = open("static" + env["PATH_INFO"]) #
          jpegdata = jpegfile.read()
          start_response('200 OK', [('Content-Type', 'image/jpeg'),
               ('Accept-Ranges', 'bytes'),
               ('Content-Length', str(len(jpegdata))),
               ('Connection', 'close')])
          return [jpegdata]
  except Exception as e:
      return ['&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;meta charset="utf-8"&gt;&lt;title&gt;Oops',
              "&lt;/title&gt;Can't read file on server!&lt;/html&gt;"]
  </code></pre>
  
  <p>-- AFAICS a WSGI app has to return an iterable containing strings; images are bytes objects, but the server is running Python 2.7, &amp; 'bytes' is an alias for 'string' -- so that shouldn't be a problem. I can't find any info on encoding in this situation, &amp; HTTP handles octets. I've tried all sorts of variations &amp; Googled (R) this for days, but still stuck. How do I get this silly thing to serve an image?</p>