test_captcha.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python3
  2. """
  3. 测试验证码API
  4. """
  5. import requests
  6. import json
  7. def test_captcha():
  8. """测试验证码"""
  9. url = "http://localhost:8000/api/v1/auth/captcha"
  10. print("测试验证码API...")
  11. print(f"URL: {url}")
  12. try:
  13. response = requests.get(url, timeout=10)
  14. print(f"\n状态码: {response.status_code}")
  15. if response.status_code == 200:
  16. result = response.json()
  17. print(f"响应码: {result.get('code')}")
  18. print(f"消息: {result.get('message')}")
  19. data = result.get('data', {})
  20. captcha_id = data.get('captcha_id')
  21. captcha_image = data.get('captcha_image', '')
  22. captcha_text = data.get('captcha_text')
  23. print(f"验证码ID: {captcha_id}")
  24. print(f"验证码文本: {captcha_text}")
  25. print(f"图片格式: {captcha_image[:50]}...")
  26. if result.get('code') == 0:
  27. print("\n✅ 验证码API正常工作!")
  28. else:
  29. print(f"\n❌ API返回错误: {result.get('message')}")
  30. else:
  31. print(f"\n❌ HTTP错误: {response.status_code}")
  32. print(f"响应内容: {response.text}")
  33. except requests.exceptions.ConnectionError:
  34. print("\n❌ 无法连接到服务器,请确保后端服务正在运行")
  35. except Exception as e:
  36. print(f"\n❌ 测试失败: {e}")
  37. if __name__ == "__main__":
  38. test_captcha()