#!/usr/bin/env python3 """ 测试验证码API """ import requests import json def test_captcha(): """测试验证码""" url = "http://localhost:8000/api/v1/auth/captcha" print("测试验证码API...") print(f"URL: {url}") try: response = requests.get(url, timeout=10) print(f"\n状态码: {response.status_code}") if response.status_code == 200: result = response.json() print(f"响应码: {result.get('code')}") print(f"消息: {result.get('message')}") data = result.get('data', {}) captcha_id = data.get('captcha_id') captcha_image = data.get('captcha_image', '') captcha_text = data.get('captcha_text') print(f"验证码ID: {captcha_id}") print(f"验证码文本: {captcha_text}") print(f"图片格式: {captcha_image[:50]}...") if result.get('code') == 0: print("\n✅ 验证码API正常工作!") else: print(f"\n❌ API返回错误: {result.get('message')}") else: print(f"\n❌ HTTP错误: {response.status_code}") print(f"响应内容: {response.text}") except requests.exceptions.ConnectionError: print("\n❌ 无法连接到服务器,请确保后端服务正在运行") except Exception as e: print(f"\n❌ 测试失败: {e}") if __name__ == "__main__": test_captcha()