简体   繁体   中英

variable substitution in python twisted static file

How can you substitute a twisted server variable into the file resource that is served out.

For example, the following code serves up a webpage where i can go and load up ./templates/index.html :

if __name__ == '__main__':
    var = 'my variable'
    from twisted.web.static import File
    webdir = File("{0}/templates/".format(os.path.dirname(os.path.realpath(__file__))))
    web = Site(webdir)
    reactor.listenTCP(int(os.environ.get('SR_LISTEN_PORT')), web)
    reactor.run()

I want the variable 'var' to be substituted for {{variable}} in a basic index.html page

so the page would render 'my variable' instead of hello world for instance.

How can I accomplish this?

It looks like you need a template engine for serving files, you can use jinja2 for it. In your case static.File should be used to render template directory and resource.Resource — to serve files in it rendered via jinja2:

import os

import jinja2

from twisted.internet import reactor
from twisted.web.resource import Resource
from twisted.web.static import File
from twisted.web.server import Site

template_dir = '{}/templates/'.format(os.path.dirname(os.path.realpath(__file__)))

def render(template_file, context):
    env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))
    return env.get_template(template_file).render(context).encode('utf-8')

class RDirectory(File):
    def getChild(self, name, request):
        if name == '':
            return super(RDirectory, self).getChild(name, request)
        return RFile()

class RFile(Resource):
    isLeaf = True

    def render_GET(self, request):
        data = {'var': 'my variable'}
        return render(request.uri, data)

if __name__ == '__main__':
    web = Site(RDirectory(template_dir))
    reactor.listenTCP(int(os.environ.get('SR_LISTEN_PORT')), web)
    reactor.run()

File can be something like this:

<html>
   <head><title>Test</title></head>
   <body>{{ var }}</body>
</html>

Note that jinja2 rendering is a synchronous operation and it will block twisted reactor. To avoid it you can launch rendering in a thread or try to use built-in twisted templates but they do not provide many possibilities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM