【问题标题】:Python | How to test for human for a Command Line Interface (CLI)蟒蛇 |如何为命令行界面 (CLI) 进行人工测试
【发布时间】:2021-12-14 03:25:41
【问题描述】:

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

不应该依赖远程服务

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

【问题讨论】:

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

标签: python captcha command-line-interface


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    #! /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 代码。

    【讨论】:

      【解决方案3】:

      我想到了使用pyfiglet 模块来解决这个问题。想象一下,作为人类很容易通过,并且很容易将检查添加到您的 Python 中,但很难使用脚本自动解析输出。

      # File: captch.py
      
      import pyfiglet
      import random
      import sys
      
      
      def main():
          check_number = random.randint(0, 9999)
          check_string = f"{check_number:>04}"
          print(pyfiglet.Figlet().renderText(check_string))
          response = input("Please enter the number shown above to proceed: ")
          if response != check_string:
              print("Failed verification check!", file=sys.stderr)
              exit(-1)
          print("Passed verification check!")
      
      
      if __name__ == "__main__":
          main()
      
      

      使用上述代码的通过验证示例:

      $ python captcha.py 
       _____ _  _  _____ ___  
      |___ /| || ||___ // _ \ 
        |_ \| || |_ |_ \ (_) |
       ___) |__   _|__) \__, |
      |____/   |_||____/  /_/ 
                              
      
      Please enter the number shown above to proceed: 3439
      Passed verification check!
      

      使用上述代码的失败验证示例:

      $ python captcha.py 
       ____ _____ ___ _____ 
      |___ \___  / _ \___ / 
        __) | / / | | ||_ \ 
       / __/ / /| |_| |__) |
      |_____/_/  \___/____/ 
                            
      
      Please enter the number shown above to proceed: 1234
      Failed verification check!
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-07
        • 1970-01-01
        • 2017-05-03
        • 1970-01-01
        • 2022-12-15
        • 2013-02-23
        相关资源
        最近更新 更多