71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
"""短信宝(smsbao.com)发送测试脚本
|
|
|
|
接口文档:https://www.smsbao.com/openapi
|
|
发送:GET https://api.smsbao.com/sms?u=用户名&p=ApiKey&m=手机号&c=内容(UTF-8 URL Encode)
|
|
返回:'0' 提交成功,其它为错误码(30 密码错/41 余额不足/43 IP限制/50 敏感词/51 号码错误)
|
|
余额:GET https://www.smsbao.com/query?u=用户名&p=ApiKey → 第二行为「发送条数,剩余条数」
|
|
"""
|
|
import random
|
|
|
|
import requests
|
|
|
|
# ---------- 接口配置 ----------
|
|
USERNAME = "followwin"
|
|
API_KEY = "325edeb8e17f4c00b03a0294a8d794e3"
|
|
SEND_URL = "https://api.smsbao.com/sms"
|
|
QUERY_URL = "https://www.smsbao.com/query"
|
|
|
|
SIGN_TAG = "【followwin】" # 短信签名(固定)
|
|
CONTENT = "您的验证码是{code}。如非本人操作,请忽略本短信" # 短信正文
|
|
|
|
# ---------- 测试参数:改成你要接收短信的手机号 ----------
|
|
MOBILE = "18578924566"
|
|
|
|
# 常见返回码说明(便于自查)
|
|
ERROR_CODES = {
|
|
"30": "错误密码 / ApiKey 不对",
|
|
"40": "账号不存在",
|
|
"41": "余额不足",
|
|
"42": "账号已过期",
|
|
"43": "IP 地址限制",
|
|
"50": "内容含有敏感词",
|
|
"51": "手机号码不正确",
|
|
"60": "尚未充值,接口不可调用",
|
|
}
|
|
|
|
|
|
def query_balance() -> str:
|
|
"""查询余额:返回「发送条数,剩余条数」"""
|
|
resp = requests.get(QUERY_URL, params={"u": USERNAME, "p": API_KEY}, timeout=15)
|
|
resp.raise_for_status()
|
|
lines = resp.text.strip().splitlines()
|
|
if lines and lines[0].strip() == "0":
|
|
return lines[1].strip() if len(lines) > 1 else "0,0"
|
|
return f"查询失败:{lines[0] if lines else '空响应'}"
|
|
|
|
|
|
def send_sms(mobile: str) -> dict:
|
|
"""发送验证码短信;返回 {result, code, ok}"""
|
|
code = random.randint(100000, 999999)
|
|
params = {
|
|
"u": USERNAME,
|
|
"p": API_KEY,
|
|
"m": mobile,
|
|
"c": SIGN_TAG + CONTENT.format(code=code),
|
|
}
|
|
resp = requests.get(SEND_URL, params=params, timeout=30)
|
|
resp.raise_for_status()
|
|
result = resp.text.strip()
|
|
return {
|
|
"result": result,
|
|
"code": code, # 测试期附带,便于比对收到的短信
|
|
"ok": result == "0",
|
|
"detail": "" if result == "0" else ERROR_CODES.get(result, "未知错误"),
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("当前余额(发送条数,剩余条数):", query_balance())
|
|
print(f"发送到 {MOBILE} …")
|
|
print(send_sms(MOBILE))
|