Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
webpy app going in circles
#1
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:
Reply


Messages In This Thread
webpy app going in circles - by kintarowonders - Feb-25-2019, 03:16 PM
RE: webpy app going in circles - by Larz60+ - Feb-25-2019, 03:45 PM
RE: webpy app going in circles - by kintarowonders - Feb-25-2019, 03:46 PM
RE: webpy app going in circles - by Larz60+ - Feb-25-2019, 03:58 PM
RE: webpy app going in circles - by kintarowonders - Feb-25-2019, 04:08 PM
RE: webpy app going in circles - by snippsat - Feb-25-2019, 06:00 PM
RE: webpy app going in circles - by kintarowonders - Feb-25-2019, 06:58 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020