Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
web.py: implementing a captcha
#1
I am trying to make it so users have to enter a captcha to enter a site. I am new to webpy, and I've got this code and I don't even know why it doesn't work. Can someone point me in the right direction?

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', '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"):
        cookie = web.cookies().get('captchad')
        form = enquiry_form()
        if not cookie:
            return render.capform(form)
        args = argu.split('/')
        firstname = args[0]
        if (len(args) >= 2):
            lastname = args[1]
            return render.index(firstname, lastname)
        return render.index(firstname, "Snow")
    def POST(self):
        form = enquiry_form()
        if not form.validates():
            return render.capform(form)
        else:
            data = web.input()
            if (data.captcha == session.captcha):
                web.setcookie('captchad', 'yes', 300)

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()
It always says the captcha is incorrect, and it should when the captcha matches set a cookie which stops the captcha form from displaying.

All I know is whatever I'm supposed to do goes at line 46.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Automating Captcha form submission with Mechanize Dexty 2 3,257 Aug-03-2021, 01:02 PM
Last Post: Dexty
  Parsing html page and working with checkbox (on a captcha) straannick 17 11,041 Feb-04-2021, 02:54 PM
Last Post: snippsat
  Captcha from other site GoTo95 1 2,862 Nov-13-2018, 03:48 PM
Last Post: j.crater
  Flask - Implementing SQLAlchemy with Blueprints jolsendev 1 10,055 Nov-01-2018, 05:38 PM
Last Post: jolsendev

Forum Jump:

User Panel Messages

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