test.py:
import web
render = web.template.render('templates/')
urls = (
'/', 'index'
)
class index:
def GET(self):
name='Bob'
return render.test(name)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
templates / test.html:
$def with (name)
$if name:
I just wanted to say <em>hello</em> to $name.
$else:
<em>Hello</em>, world!
环境:Python38 x64,Windows 10,Web.py == 0.4
错误详细信息:
raise SecurityError("\n".join([str(err) for err in self.errors]))
web.template.SecurityError: templates\test.html:3 - execution of 'Constant' statements is denied
templates\test.html:7 - execution of 'Constant' statements is denied
templates\test.html:7 - execution of 'Constant' statements is denied
templates\test.html:7 - execution of 'Constant' statements is denied
templates\test.html:9 - execution of 'Constant' statements is denied
找到解决方案:
How to fix "execution of 'Constant' statements is denied" error?
如pbuck所建议,只需添加:
from web.template import ALLOWED_AST_NODES
ALLOWED_AST_NODES.append('Constant')
并且有效!
问题:
为什么在test.html中不允许我的特定语句(不确定pbuck的答案中“'Constant'是python3 AST中的节点,而不是python2中的节点)”
我的代码有问题吗?
为什么/解决方案如何工作?
这不是您代码中的错误,而是一个兼容性问题,应在web.py中修复。
AST是抽象语法树-python在解析python代码时创建的内部结构(您的模板通过web.py转换为python代码,然后执行)。
语法树的一部分指示了所有内容,包括语句,表达式,参数,运算符等(请参见https://docs.python.org/3/library/ast.html#ast.parse)……但您实际上并不需要知道所有这些。
解析的结果web.py会确保(出于安全原因)仅使用“允许的”节点类型。也就是说,并非所有合法的python构造在web.py模板中都是合法的。 (为什么?不知道,这就是web.py的设计方式。)
web.py从python2开始,并移植到python3。 python3和3.8中引入了“常量”节点,现在所有常量都使用“常量”。 (为什么?击败我了-这是内部的python魔术。)这似乎是web.py应该更新为允许模板中的Constant节点。
直到web.py版本更新,您才可以更新代码,这样它就可以正常工作。