sub2api批量账号注册脚本
从需求到交付:AI辅助开发的完整实战
2026.05.23
使用 → 或 空格 翻页 按 ESC 查看缩略图
users.txt 读取姓名列表
API
POST /api/v1/admin/users
输入
users.txt(中文姓名)
输出
accounts.txt(账号密码)
用户向 AI 提出的原始需求
用户提供的 curl 命令
curl 'https://sub2api.toahui.cn/api/v1/admin/users' \
-H 'accept: application/json, text/plain, */*' \
-H 'authorization: Bearer eyJhbG...SeYQ' \
-H 'content-type: application/json' \
--data-raw '{"email":"test@qq.com","password":"123456",
"username":"","notes":"","balance":0,
"concurrency":1,"rpm_limit":0}'
问题 手动执行14次?改14次email和密码?不可扩展
AI第一版输出:手写拼音映射表(部分)
pinyin_map = {
'姚': 'yao', '志': 'zhi',
'张': 'zhang', '伟': 'wei', ...
}
def chinese_to_pinyin(name):
result = []
for char in name:
if char in pinyin_map:
result.append(pinyin_map[char])
else:
result.append(char.lower())
return ''.join(result)
缺陷 200+行硬编码映射,遇到生僻字直接跳过,维护困难
pinyin_map = {
'姚': 'yao',
'志': 'zhi',
'张': 'zhang',
# ... 200+ 行
}
from pypinyin import lazy_pinyin
def name_to_pinyin(name):
parts = lazy_pinyin(name)
return "".join(parts).lower()
收益 代码从200+行降到3行,支持所有汉字,零维护成本
用户反馈:结果保存时,如果有记得备份
import os
from datetime import datetime
output_file = "accounts.txt"
if os.path.exists(output_file):
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
backup = f"{output_file}.{ts}"
os.rename(output_file, backup)
print(f"旧文件已备份: {backup}")
效果
accounts.txt.20260523_095533
每次运行自动保留历史记录,不怕数据丢失
用户反馈:结果第一列加上原中文名
yaozhi@sclead.com 0xDvfMN55l 成功
姚志 yaozhi@sclead.com RgKB9O4n2Q 成功
# 核心改动:results 存储时加入 name
results.append((name, email, password, "成功"))
# 写入时输出4列
f.write(f"{name}\t{email}\t{password}\t{status}\n")
Python 3.9 不支持 | 联合类型语法
def register_user(email: str, password: str)
-> tuple[int | None, str]:
...
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
def register_user(email, password):
...
移除类型注解,兼容 Python 3.9
教训 AI生成代码时默认使用最新Python语法,需确认运行环境版本
改了数据结构,忘了改统计代码
# results 已改为 (name, email, pwd, status)
results.append((name, email, password, "成功"))
# 但统计还是按3个元素解包
success = sum(1 for _, _, s in results if s == "成功")
# ValueError: too many values to unpack
# 改为4个元素解包
success = sum(1 for _, _, _, s
in results if s == "成功")
教训 修改数据结构时,确保所有使用该结构的地方同步更新
#!/usr/bin/env python3
import os, secrets, string, time
from datetime import datetime
import requests
from pypinyin import lazy_pinyin
API_URL = "https://sub2api.toahui.cn/api/v1/admin/users"
HEADERS = { ... } # Authorization + Content-Type
def generate_password(length=10):
chars = string.ascii_letters + string.digits
return "".join(secrets.choice(chars) for _ in range(length))
def read_names(filepath):
with open(filepath, "r", encoding="utf-8") as f:
return [line.strip() for line in f if line.strip()]
def name_to_pinyin(name):
return "".join(lazy_pinyin(name)).lower()
def register_user(email, password):
payload = {
"email": email, "password": password,
"username": "", "notes": "",
"balance": 9999, "concurrency": 20, "rpm_limit": 0
}
resp = requests.post(API_URL, headers=HEADERS, json=payload, timeout=30)
return resp.status_code, resp.text
def main():
names = read_names("users.txt")
results = []
for name in names:
email = f"{name_to_pinyin(name)}@sclead.com"
password = generate_password(10)
status, text = register_user(email, password)
results.append((name, email, password,
"成功" if status == 200 else f"失败-{status}"))
time.sleep(0.5)
# 自动备份
if os.path.exists("accounts.txt"):
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
os.rename("accounts.txt", f"accounts.txt.{ts}")
with open("accounts.txt", "w") as f:
for r in results:
f.write(f"{r[0]}\t{r[1]}\t{r[2]}\t{r[3]}\n")
if __name__ == "__main__":
main()
终端输出
共读取到 14 个用户,开始注册...
注册: 陈云虎 -> chenyunhu@sclead.com
失败 [409]: email already exists
注册: 黄水清 -> huangshuiqing@sclead.com
失败 [409]: email already exists
...
旧文件已备份: accounts.txt.20260523_095910
完成!成功: 0/14
结果已保存到 accounts.txt
accounts.txt
陈云虎 chenyunhu@sclead.com RgKB9O4n2Q 失败-409
黄水清 huangshuiqing@sclead.com VeyGwbpB2G 失败-409
刘明升 liumingsheng@sclead.com uEkpQjBwMI2 失败-409
...
姚志 yaozhi@sclead.com 0xDvfMN55l 成功
注意 409 = 邮箱已存在(正常,因为账号之前已注册过)
AI不是替代程序员,而是让程序员专注于
需求理解、
架构设计、
业务逻辑
而不是语法细节
AI + 人 = 更快的交付,更好的代码
如果你完全信任AI,也可以让"他"放飞