Python |如何为命令行界面(CLI)测试人类

时间:2014-07-29 13:34:32

标签: python captcha command-line-interface

我正在编写一个python CLI - 在某些情况下我想测试用户是一个人。基本上为CLI实现Captcha。

它不应该依赖于远程服务

有没有人有一个优雅的解决方案?

2 个答案:

答案 0 :(得分:2)

ASCII艺术怎么样?还有一个问题:这个图像上显示的是谁或者是什么?

请参阅https://www.google.de/search?q=ascii+art

答案 1 :(得分:1)

您的脚本中可能有一组问题:

#! /usr/bin/env python
# encoding: utf-8

import sys
import time
import random
import hashlib

def ask_question():
  questions = [
    ('What animal has a trunk?', ('1c6f116ce35bbe8b5c5b3a26cfa9e63c4b7cff24', '0ae9e4deba26021986ffd99636da6601f6393631')),
    ('How many continents are there?', ('902ba3cda1883801594b6e1b452790cc53948fda')),
    ('Where is Big Ben?', ('707fe00aa123eb0be5010f1d3065c2b6d7934ca4', '4c57f0c88d9844630327623633ce269cf826ab99'))
  ]
  random.seed(time.time())
  question = questions[random.randint(0, len(questions) - 1)]
  answers = question[1]
  question = question[0]
  sys.stdout.write(question + '\n')
  try:
    user = input('Answer: ')
  except NameError:
    user = raw_input('Answer: ')
  sha1 = hashlib.new('sha1')
  sha1.update(user.encode())
  if sha1.hexdigest() not in answers:
    sys.stderr.write('Not a correct answer\n')
    sys.exit(1)

ask_question()

用作:

[matt tests] ./questions.py 
What animal has a trunk?
Answer: Elephant
[matt tests] ./questions.py 
What animal has a trunk?
Answer: Wolf
Not a correct answer
[matt tests]

SHA1是这样的,用户无法通过加载脚本来解析答案。理想情况下,你会为​​每个哈希添加一个盐来防止蛮力,但这样做太过分了,因为你总是可以修改python代码。