Python twisted web:如何正确地从图像目录中删除图像

Python twisted web:如何正确地从图像目录中删除图像,python,webserver,twisted,Python,Webserver,Twisted,我已经成功地编写了一个twisted web服务器,它可以显示动态生成的内容。 但是,当我试图显示目录(~cwd/plots/image.png)中的图像(png)文件时,服务器会毫无错误地失败,浏览器会显示alt文本或损坏的图像图标。我做错了什么 import sys from twisted.internet import reactor import twisted.python.filepath from twisted.web import server, resource, stat

我已经成功地编写了一个twisted web服务器,它可以显示动态生成的内容。 但是,当我试图显示目录(~cwd/plots/image.png)中的图像(png)文件时,服务器会毫无错误地失败,浏览器会显示alt文本或损坏的图像图标。我做错了什么

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()
导入系统 从twisted.internet导入 导入twisted.python.filepath 从twisted.web导入服务器、资源、静态 文件=[“plot/10.53.174.1.png”、“plot/10.53.174.2.png”、“plot/10.53.174.3.png”] 类子级(resource.resource): 定义初始化(self、链接、文件名): resource.resource.\uuuuu init\uuuuu(self) self.filename=文件名 self.link=link 通过 def呈现(自我,请求): filepath=“/%s”%(self.filename) linkpath=“%s%s”%(self.link,filepath) self.getChild(linkpath,static.File(self.filename)) 返回“” %s%s
“”“%(self.link、self.link、linkpath) 类顶级(resource.resource): #addSlash=True def呈现(自我,请求): request.write(““监视服务器列表服务器列表
    ”) #假设打印文件名的预先验证列表。无论如何都要验证他们的存在 如果文件: 对于文件中的文件名: 如果os.path.存在(文件名): 尝试: link=fileName.split('.png')[0] link=link.split('plot')[1] 路径=链接[1:] 请求。写入(“

  • ”%(链接,链接)) root.putChild(路径,子(链接,文件名)) #root.putChild(路径,子文件名)) 例外情况除外,原因如下: 请求。写入(“%s
    ”%)(str(why))) 其他: request.write(“您可能认为%s存在,但它不存在”%(文件名)) 其他: request.write(““未提供任何文件”) 请求。写入(“
”) 返回“” 如果名称=“\uuuuu main\uuuuuuuu”: root=resource.resource() 主页=顶级() png_file=twisted.python.filepath.filepath(“./plot/”) root.putChild(“”,home) 站点=服务器。站点(根) 反应堆listenTCP(8007,现场) 反应堆运行()
  • 第一:查看源代码 , 必须更改代码才能渲染 正确的图像链接
  • 第二:使用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()
导入系统 导入操作系统 从twisted.internet导入 导入twisted.python.filepath 从twisted.web.static导入文件 从twisted.web导入服务器、资源、静态 文件=[“plot/10.53.174.1.png”、“plot/10.53.174.2.png”、“plot/10.53.174.3.png”] 类子级(resource.resource): 定义初始化(self、链接、文件名): resource.resource.\uuuuu init\uuuuu(self) self.filename=文件名 self.link=link 通过 def呈现(自我,请求): filepath=“/%s”%(self.filename) linkpath=filepath #linkpath=“%s%s”%(self.link,filepath) self.getChild(linkpath,static.File(self.filename)) 返回“” %s%s
“”“%(self.link、self.link、linkpath) 类顶级(resource.resource): #addSlash=True def呈现(自我,请求): request.write(““监视服务器列表服务器列表
    ”) #假设打印文件名的预先验证列表。无论如何都要验证他们的存在 如果文件: 对于文件中的文件名: request.write(文件名) 如果os.path.存在(文件名): 尝试: link=fileName.split('.png')[0] link=link.split('plot')[1] 路径=链接[1:] 请求。写入(“

  • ”%(链接,链接)) root.putChild(路径,子(链接,文件名)) #root.putChild(路径,子文件名)) 例外情况除外,原因如下: 请求。写入(“%s
    ”%)(str(why))) 其他: request.write(“您可能认为%s存在,但它不存在”%(文件名)) 其他: request.write(““未提供任何文件”) 请求。写入(“
”) 返回“” 如果名称=“\uuuuu main\uuuuuuuu”: root=resource.resource() 主页=顶级() png_file=twisted.python.filepath.filepath(“./plot/”) 绘图资源=文件('./绘图') root.putChild(“”,home) root.putChild('plot',plot\u资源) 站点=服务器。站点(根) 反应堆listenTCP(8007,现场) 反应堆运行() 另外,你的代码有点难看。你可以改进它。试着读一下: