gpt4 book ai didi

python - 尝试运行 webpy 示例 : app isn't defined

转载 作者:行者123 更新时间:2023-12-01 01:11:18 27 4
gpt4 key购买 nike

我正在尝试使用 webpy 根据其他人的代码实现验证码。我开始的代码在这里:https://kzar.co.uk/blog/2009/07/14/web.py-captcha/

示例代码不完整,我需要弄清楚如何处理这个应用程序变量。这是我的代码:

import web
from captcha import getCaptcha

render = web.template.render('templates/')

urls = (
'/([a-zA-Z]+/[a-zA-Z]+)', 'index',
'/', 'index',
'/captcha.gif', 'captcha'
)

if web.config.get("_session") is None:
session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'captcha': ''})
web.config._session = session
else:
session = web.config._session

vcaptcha = form.Validator('Please enter the code', lambda x:x == session.captcha)

enquiry_form = form.Form(
form.Textbox("captcha", vcaptcha, description="Validation Code", pre="<img src='/captcha.gif' valign=center><br>", class_="standard", style="width:70px;"),
)

class index:
def GET(self, argu = "Anonymous/Person"):
args = argu.split('/')
firstname = args[0]
if (len(args) >= 2):
lastname = args[1]
return render.index(firstname, lastname)
return render.index(firstname, "Snow")

class captcha:
def GET(self):
web.header("Content-Type", "image/gif")
captcha = getCaptcha
session.captcha = captcha[0]
return captcha[1].read()

if __name__ == "__main__":
app = web.application(urls, globals())
app.run()

运行时出现此错误:

$ python code.py
Traceback (most recent call last):
File "code.py", line 13, in <module>
session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'captcha': ''})
NameError: name 'app' is not defined

我一直在查看 webpy 文档和 API 引用,但我不知道如何正确初始化这个“app”变量。

最佳答案

当您调用 session = web.session.Session(app, ... 时,您正在使用尚未定义的 app您看过sessions上的文档吗? ?在使用之前,请先了解他们如何在示例中定义 app

关于python - 尝试运行 webpy 示例 : app isn't defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54847795/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com