title: twisted web: how do I properly images from an images directory
body: <p>I've successfully written a twisted web server that displays dynamically generated content.
  However, when I attempt to display image (png) files from a directory (~cwd/plots/image.png), the server fails without error and the browser either displays the alt text or the broken image icon.  What am I doing wrong?</p>
  
  <pre><code>import sys
  from twisted.internet import reactor
  import twisted.python.filepath
  from twisted.web import server, resource, static
  
  FILES = ["plot/10.53.174.1.png", "plot/10.53.174.2.png", "plot/10.53.174.3.png"]
  
  class Child(resource.Resource):
  
      def __init__(self,link, filename):
          resource.Resource.__init__(self)
          self.filename = filename
          self.link = link
          pass
  
      def render(self, request ):
  
          filepath = "/%s" % (self.filename)
          linkpath = "%s%s" % (self.link, filepath)
  
          self.getChild(linkpath, static.File(self.filename))
  
  
          return """ 
          &lt;html&gt;&lt;body&gt; &lt;head&gt; &lt;title&gt;%s&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;%s&lt;/h1&gt;&lt;br&gt; &lt;img src="%s" alt = "angry beaver" /&gt; &lt;/body&gt;""" % (self.link, self.link, linkpath)
  
  class Toplevel(resource.Resource):
      #addSlash = True
  
      def render(self, request):
  
  
          request.write("""&lt;html&gt;&lt;body&gt; &lt;head&gt; &lt;title&gt;monitor server listing&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Server List&lt;/h1&gt; &lt;ul&gt;""" )
  
  
          #assume a pre-validated list of  plot file names.  validate their presence anyway
          if FILES:
              for fileName in FILES:
                  if os.path.exists(fileName):
                      try:
  
                          link =  fileName.split('.png')[0]
                          link =  link.split('plot')[1]
                          path = link[1:]
  
                          request.write('&lt;li&gt; &lt;a href="%s"&gt;%s&lt;/a&gt;&lt;br&gt;' % (link, link))
  
  
                          root.putChild(path, Child(link, fileName))
                          #root.putChild(path, Child(fileName))
  
  
                      except Exception as why:
                          request.write("%s &lt;br&gt;" % (str(why)))
                  else:
                      request.write(" you may think %s exists but it doesn't " % (fileName))
  
          else:
              request.write("""No files given""")
  
          request.write("""&lt;/ul&gt;&lt;/body&gt;&lt;/html&gt;""")
  
          return ""
  
  
  
  
  
  
  if __name__ == "__main__":
  
      root = resource.Resource()
      home = Toplevel()
  
      png_file = twisted.python.filepath.FilePath('./plot/')
  
      root.putChild('', home)
  
      site = server.Site(root)
      reactor.listenTCP(8007, site)
      reactor.run()
  </code></pre>