2 from twisted.web2 import server, http, resource, channel
3 from twisted.web2 import static, http_headers, responsecode
5 class FileDownloader(static.File):
7 resp = super(FileDownloader, self).render(req)
8 if resp != responsecode.NOT_FOUND:
13 {'content-type': http_headers.MimeType('text', 'html')},
16 <p>TODO: eventually this will trigger a search for that file.</body></html>""")
19 class Toplevel(resource.Resource):
22 def __init__(self, directory):
23 self.directory = directory
26 def addDirectory(self, directory):
27 path = "~" + str(len(self.subdirs))
28 self.subdirs.append(directory)
31 def removeDirectory(self, directory):
32 loc = self.subdirs.index(directory)
33 self.subdirs[loc] = ''
35 def render(self, ctx):
38 {'content-type': http_headers.MimeType('text', 'html')},
41 <p>TODO: eventually some stats will be shown here.</body></html>""")
43 def locateChild(self, request, segments):
45 if len(name) > 1 and name[0] == '~':
51 if loc >= 0 and loc < len(self.subdirs) and self.subdirs[loc]:
52 return static.File(self.subdirs[loc]), segments[1:]
57 return FileDownloader(self.directory), segments[0:]
61 if __name__ == '__builtin__':
62 # Running from twistd -y
64 t.addDirectory('/tmp')
65 t.addDirectory('/var/log')
68 # Standard twisted application Boilerplate
69 from twisted.application import service, strports
70 application = service.Application("demoserver")
71 s = strports.service('tcp:18080', channel.HTTPFactory(site))
72 s.setServiceParent(application)