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

Python |如何为命令行界面(CLI)测试人员,python,captcha,command-line-interface,Python,Captcha,Command Line Interface,我正在编写一个python CLI——在某些情况下,我想测试用户是否是人类。基本上是为CLI实现验证码 它不应该依赖于远程服务 有人有优雅的解决方案吗?ASCII艺术呢?还有一个问题:这张图片上显示的是谁或什么 请参见脚本中可能有一组问题: #! /usr/bin/env python # encoding: utf-8 import sys import time import random import hashlib def ask_question(): questions =

我正在编写一个python CLI——在某些情况下,我想测试用户是否是人类。基本上是为CLI实现验证码

它不应该依赖于远程服务


有人有优雅的解决方案吗?

ASCII艺术呢?还有一个问题:这张图片上显示的是谁或什么


请参见

脚本中可能有一组问题:

#! /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是这样的,用户无法通过加载脚本来解析答案。理想情况下,您可以在每个哈希中添加一个salt来防止暴力,但这太过分了,因为您总是可以修改python代码。

而不是对人进行测试,难道您不能让人或机器自我识别吗?例如:
mycommand--human=true
mycommand--human=false
?我想他想测试他是面对另一端的人还是机器人,类似于Captch的工作原理。。。在您的解决方案中,任何机器人都可能伪装成人类。如何阻止具有CLI访问权限的人复制您的Python脚本编辑“captcha”并运行该脚本?一般来说,为了运行Python脚本而访问CLI(即shell)的人也可以完全访问脚本所做的一切,因此可以编写等效的脚本。是的,什么都没有。我知道这一点。我应该为问题陈述提供证据。人们对CLI中的提示不敏感:只需按“Y”键即可移动脚本。在进行潜在危险的操作之前,我真的希望用户在继续之前思考一下,让他们键入“是”是解决该特定问题的常用方法。另一种方法是让他们输入(再次)他们想要删除或销毁的内容的名称。一般来说,让他们回答一个简单的数学问题就可以了,但用户不一定会接受。