2010-11-05 102 views
3

我已經成功地編寫了一個顯示動態生成內容的扭曲網頁服務器。 但是,當我嘗試從目錄(〜cwd/plots/image.png)顯示圖像(png)文件時,服務器將無故障地失敗,並且瀏覽器顯示替代文本或損壞的圖像圖標。我究竟做錯了什麼?扭曲的網頁:我如何從圖像目錄中正確顯示圖像

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 """ 
     <html><body> <head> <title>%s</title> </head> <body> <h1>%s</h1><br> <img src="%s" alt = "angry beaver" /> </body>""" % (self.link, self.link, linkpath) 

class Toplevel(resource.Resource): 
    #addSlash = True 

    def render(self, request): 


     request.write("""<html><body> <head> <title>monitor server listing</title> </head> <body> <h1>Server List</h1> <ul>""") 


     #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('<li> <a href="%s">%s</a><br>' % (link, link)) 


         root.putChild(path, Child(link, fileName)) 
         #root.putChild(path, Child(fileName)) 


        except Exception as why: 
         request.write("%s <br>" % (str(why))) 
       else: 
        request.write(" you may think %s exists but it doesn't " % (fileName)) 

     else: 
      request.write("""No files given""") 

     request.write("""</ul></body></html>""") 

     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() 

回答

1
  • 首先:查看源在 http://localhost:8007/10.53.174.1, 必須更改代碼,以使 正確的鏈接到圖像。
  • 二:使用twisted.web.static.File到 提供靜態內容

更正代碼:

import sys 
import os 
from twisted.internet import reactor 
import twisted.python.filepath 
from twisted.web.static import File 
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 = filepath 
     #linkpath = "%s%s" % (self.link, filepath) 

     self.getChild(linkpath, static.File(self.filename)) 


     return """ 
     <html><body> <head> <title>%s</title> </head> <body> <h1>%s</h1><br> <img src="%s" alt = "angry beaver" /> </body>""" % (self.link, self.link, linkpath) 

class Toplevel(resource.Resource): 
    #addSlash = True 

    def render(self, request): 


     request.write("""<html><body> <head> <title>monitor server listing</title> </head> <body> <h1>Server List</h1> <ul>""") 


     #assume a pre-validated list of plot file names. validate their presence anyway 
     if FILES: 
      for fileName in FILES: 
     request.write(fileName) 
       if os.path.exists(fileName): 
        try: 

         link = fileName.split('.png')[0] 
         link = link.split('plot')[1] 
         path = link[1:] 

         request.write('<li> <a href="%s">%s</a><br>' % (link, link)) 


         root.putChild(path, Child(link, fileName)) 
         #root.putChild(path, Child(fileName)) 


        except Exception as why: 
         request.write("%s <br>" % (str(why))) 
       else: 
        request.write(" you may think %s exists but it doesn't " % (fileName)) 

     else: 
      request.write("""No files given""") 

     request.write("""</ul></body></html>""") 

     return "" 






if __name__ == "__main__": 

    root = resource.Resource() 
    home = Toplevel() 

    png_file = twisted.python.filepath.FilePath('./plot/') 

    plot_resource = File('./plot') 

    root.putChild('', home) 
    root.putChild('plot', plot_resource) 

    site = server.Site(root) 
    reactor.listenTCP(8007, site) 
    reactor.run() 

PS。你的代碼很難看。你可以改進它。嘗試閱讀:

+0

我花了一段時間來工作,但是這就是答案。 – John 2012-02-01 15:24:38