"""This server implementation still allows multiple variants of
the same CAPTCHA word to be requested."""
import web
from Captcha.Visual import Text, Backgrounds, Distortions, ImageCaptcha
from Captcha import Words
import pyDes, base64
urls = ('/', 'index', '/(.*).jpg', 'image')
ids = []
k = pyDes.des('s3kritss', pyDes.ECB)
def encode(s):
return base64.b64encode(k.encrypt(s, ' '))
def decode(s):
return k.decrypt(base64.b64decode(s), ' ')
class WeakCaptcha(ImageCaptcha):
def __init__(self, word):
self.word = word
ImageCaptcha.__init__(self)
def getLayers(self):
self.addSolution(self.word)
return [ Backgrounds.CroppedImage(),
Text.TextLayer(self.solutions[0]),
Distortions.SineWarp() ]
class index:
def GET(self):
id = encode(Words.defaultWordList.pick())
ids.append(id)
print """<html><body>
<p><img src="%s.jpg"/></p>
<p><form action="/" method="post">
Enter the word shown:
<input type="hidden" name="id" value="%s" />
<input type="text" name="word" />
</form></p>
</body></html>
""" % (id, id)
def POST(self):
i = web.input('id', 'word')
if (i.id in ids) and (decode(i.id) == i.word):
print "Passed"
ids.remove(i.id)
else:
print "Fail!"
class image:
def GET(self, id):
print WeakCaptcha(decode(id)).render().tostring('jpeg', 'RGB')
if __name__ == '__main__': web.run(urls, globals(), web.reloader)