Feb-25-2019, 03:16 PM
(This post was last modified: Feb-25-2019, 03:17 PM by kintarowonders.)
I am working on a web.py app which makes it so a captcha has to be given to see a page. I found some code with Google where someone had aleady done this and made it fit my app. However, it is kind of vague, and I can't seem to get it work. Even when I enter the captcha properly, it says incorrect captcha. I've done something wrong, but web.py isn't well documented and learning it seems all about changing stuff and seeing what happens. I am absolutely stumped at this point as to why the captcha doesn't validate.
import web from web import form from captcha import getCaptcha render = web.template.render('templates/') urls = ( '/([a-zA-Z]+/[a-zA-Z]+)', 'index', '/', 'index', '/captcha.gif', 'captchaimg', '/captcha', 'captcha' ) app = web.application(urls, globals()) 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"): try: if (not session.captchad): web.seeother("/captcha") except AttributeError: web.seeother("/captcha") 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): f = enquiry_form() return render.captcha(f) def POST(self): f = enquiry_form() if not f.validates(): return render.captcha(f) else: session.captchad = True web.seeother("/") class captchaimg: 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()I believe the problem might be with line 22 which I barely understand: