Feb-24-2019, 02:40 AM
(This post was last modified: Feb-24-2019, 02:40 AM by kintarowonders.)
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?
All I know is whatever I'm supposed to do goes at line 46.
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.