def opt_path(self, path): """A path that will be used to serve the root resource as a raw file or directory. """ if self['root']: raise usage.UsageError("You may only have one root resource.") self['root'] = static.File(os.path.abspath(path))
class Toplevel(resource.Resource): addSlash = True child_monkey = static.File(os.path.dirname(static.__file__) + '/static.py') child_elephant = Child() def render(self, ctx): return http.Response( 200, {'content-type': http_headers.MimeType('text', 'html')}, """<html><body> <a href="monkey">The source code of twisted.web2.static</a><br> <a href="elephant">A defined child</a></body></html>""")
class Toplevel(resource.Resource): # addSlash=True to make sure it's treated as a directory-like resource addSlash=True # Render the resource. Here the stream is a string, which will get # adapted to a MemoryStream object. def render(self, req): contents = """<html> <head><title>Twisted.web2 demo server</title><head> <body> Hello! This is a twisted.web2 demo. <ul> <li><a href="file">Static File</a></li> <li><a href="dir/">Static dir listing</a></li> <li><a href="sleepy">Resource that takes time to render</a></li> <li><a href="wsgi">WSGI app</a></li> <li><a href="cgi">CGI app</a></li> <li><a href="forms">Forms</a></li> </ul> </body> </html>""" return http.Response( responsecode.OK, {'content-type': http_headers.MimeType('text', 'html')}, contents) # Add some child resources child_file = static.File(os.path.join(os.path.dirname(resource.__file__), 'TODO')) child_dir = static.File('.') child_sleepy = Sleepy() child_wsgi = wsgi.WSGIResource(simple_wsgi_app) child_cgi = twcgi.FilteredScript(pycgi.__file__, filters=["/usr/bin/python"]) child_forms = FormPost()
def render(self, ctx): try: session = sessions[ctx.args['session'][0]] except KeyError: return http.Response(code=404, stream = "Invalid session.") try: cell_id = int(ctx.args['cell'][0]) file_name = ctx.args['file'][0] except KeyError: return http.Response(code=404, stream = "Unspecified cell or file.") cell = session.worksheet.get_cell_with_id(cell_id) if file_name in cell.files(): return static.File(os.path.join(cell.directory(), file_name)) else: return http.Response(code=404, stream = "No such file %s in cell %s." % (file_name, cell_id))
def opt_vhost_static(self, virtualHost): """Specify a virtual host in the form of domain=path to be served as raw directory or file. """ if (self['root'] and not \ isinstance(self['root'], vhost.NameVirtualHost)): raise usage.UsageError("You can only use --vhost-static alone " "or with --vhost-class and --vhost-path") domain, path = virtualHost.split('=', 1) if not self['root']: self['root'] = vhost.NameVirtualHost() self['root'].addHost(domain, static.File(os.path.abspath(path)))
def opt_vhost_path(self, path): """Specify a directory to use for automatic named virtual hosts. It is assumed that this directory contains a series of subdirectories each representing a virtual host domain name and containing the files to be served at that domain. """ if self['root']: if not isintance(self['root'], vhost.NameVirtualHost): raise usage.UsageError("You may only have one root resource") else: self['root'] = vhost.NameVirtualHost() path = os.path.abspath(path) for name in os.listdir(path): fullname = os.path.join(path, name) self['root'].addHost(name, static.File(fullname))
def getCachedFile(self, hash, req, url, d, locations): """Try to return the file from the cache, otherwise move on to a DHT lookup. @type locations: C{list} of C{dictionary} @param locations: the files in the cache that match the hash, the dictionary contains a key 'path' whose value is a L{twisted.python.filepath.FilePath} object for the file. """ if not locations: log.msg('Failed to return file from cache: %s' % url) self.lookupHash(req, hash, url, d) return # Get the first possible location from the list file = locations.pop(0)['path'] log.msg('Returning cached file: %s' % file.path) # Get it's response resp = static.File(file.path).renderHTTP(req) if isinstance(resp, defer.Deferred): resp.addBoth(self._getCachedFile, hash, req, url, d, locations) else: self._getCachedFile(resp, hash, req, url, d, locations)
mode=mode.__name__, ) response = responsecode.OK except: data = exceptions.html_error_template().render() response = responsecode.INTERNAL_SERVER_ERROR return http.Response( response, { 'Content-Type': http_headers.MimeType('text', 'html', [('charset', 'utf-8')]) }, data) root = Root() root.putChild("static", static.File("static/")) site = server.Site(root) from sys import argv import openttd import image if 'mode_openttd.py' in argv: mode = openttd http_port = 'tcp:8119' fcgi_port = 'tcp:6531' elif 'mode_images.py' in argv: mode = image http_port = 'tcp:8118' fcgi_port = 'tcp:6532'