I've been getting a bunch of spam comments lately (I knew this day would
come). This adds a field to the comment-submission page, and verifies
that the field contains a four-digit number. Hopefully this will stop
most automated bots.
web.template.Template.globals['len'] = len
print render.noentry("%s-%s-%s" % (year, month, day))
+ def verify(self, test):
+ if len(test) != 4:
+ return False
+ try:
+ x = int(test,10)
+ except ValueError:
+ return False
+ return True
def POST(self, year, month, day):
i = web.input()
+ bottest = i.bottest.strip()
+ if not self.verify(bottest):
+ web.redirect("http://127.0.0.1/go_away_bot.html")
+ return
name = i.name.strip()
email = i.email.strip()
website = i.website.strip()
<p><input name="name" type="text" size="30" aria-required="true" />Name</p>
<p><input name="email" type="text" size="30" aria-required="true" />Email (required, but won't be posted)</p>
<p><input name="website" type="text" size="30" aria-required="false" />Website (optional)</p>
+ <p><input name="bottest" type="text" size="30" aria-required="true" />Antispam: enter a four digit number</p>
<p><textarea name="comment" cols="45" rows="8" aria-required="true"></textarea></p>
<input name="submit" type="submit" id="submit" value="Post Comment" />
</form>