2013-12-11 55 views
0

我知道有很多文字,但我不認爲這實際上是一個難以解決的問題。對於TLDR,請檢查問題的結尾。驗證碼不能在webpy上工作

所以,我試圖在我的web.py應用程序中實現基本的驗證碼形式。此代碼取自http://kzar.co.uk/blog/2009/07/14/web.py-captcha。我只是展示了我正在研究的一個基本測試示例,試圖找出我的大型應用程序中出現了什麼問題。

當我在索引頁上按回車時,即使我得到的代碼是正確的(我通過在終端上打印'captcha.py'中的'word'變量來驗證),我仍然收到一個錯誤,說我的驗證碼無效。我已經試過了所有的東西,並檢查了會話變量以及單詞變量,但無論我被告知我的代碼是錯誤的。這是整個測試應用程序。

test.py:

import web 
from web import form 
from captcha import getCaptcha 

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

urls = (
    '/', 'index', 
    '/captcha.gif', 'captcha' 
) 
app = web.application(urls, locals()) 

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: 
    # Display the create match page 
    def GET(self): 
     form = enquiry_form() 
     return render.captcha(form) 

    # Save the data  
    def POST(self): 
     form = enquiry_form() 
     if not form.validates(): # If there is an issue 
      return render.captcha(form) # Return them to the create page 
     else: 
      return "Success!" 

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() 

captcha.py:

from PIL import Image, ImageDraw, ImageFont 
import cStringIO, random 

def getCaptcha(): 
    im = Image.new("RGB", (175, 60)) 
    draw = ImageDraw.Draw(im) 

    for x in range(0, 175): 
     for y in range(0, 60): 
      draw.point((x, y), (135, 191, 107)) 

    font = ImageFont.truetype('static/ZXX Camo.otf', 50) 

    alphabet = 'abcdefghijklmnopqrstuvwxyz' 

    word = '' 
    for i in range(5): 
     word = word + alphabet[random.randint(0, len(alphabet) -1)] 

    draw.text((5, 5), word, font=font, fill=(0, 0, 0)) 

    f = cStringIO.StringIO() 
    im.save(f, "GIF") 
    f.seek(0) 
    return word, f 

captcha.html:

$def with (form) 
<head> 
    <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon"> 
</head> 
<h1>Testing Captcha</h1> 

<form name="main" method="post"> 
$:form.render()  
</form> 

字體我我正在使用可在這裏:http://dl.dropbox.com/u/20517415/ZXX.zip

我完全困惑這裏發生了什麼。我相信這裏的錯誤代碼是驗證碼lambda x:x == session.captcha。我已經嘗試過許多不同版本的這一行,似乎沒有任何工作。最令人困惑的是,我甚至可以將線路更改爲lambda x:x != session.captcha,並且它仍然不起作用。我很感謝你的幫助。我一直在看這一段時間,不知道什麼是錯的。

TLDR;我在web.py應用中使用的一些基本的類似CAPTCHA的代碼無法識別正確的代碼條目,並且我已經提供了源文件供您檢查哪些是錯誤的。

回答

0

對於那些通過谷歌搜索到的人:

解決方案很簡單。我正在重新初始化應用程序實例,這意味着我每次都創建一個新的會話,沒有session.captcha var。而且由於web.py處理驗證的方式,它沒有發出解釋存在AttributeError的錯誤。所以,我所要做的就是刪除__main__中的應用程序聲明。

此外,如果不禁用調試模式,此代碼將無法正常工作,因爲web.py會話變量很奇怪=/