调试由服务器重启修复的 Twilio SSL 错误(错误 11220)
Debugging Twilio SSL Errors (Error 11220) that are fixed by Server Restart
我有一个基本的休息 api 服务器,它使用 Twilio api 接受传入的短信和回复。
每两个月左右我就注意到我需要重新启动服务器,因为 Twilio 开始给我 SSL 握手错误。这与 https 证书没有任何关系,因为正如我所提到的,它可以通过简单的重启来修复。我正在寻求调试帮助。
工具的结构
app.py - FastApi 休息服务器。有一个 /webhook 端点可以接受 Twilio 传入消息请求。
utils.py - 包含用于通过 Twilio 发送 SMS 的方法的 Utils 文件 api。
TWILIO_CLIENT = Client(TWILIO_SID, TWILIO_AUTH_TOKEN)
async def send_twilio_sms(twilio_message: TwilioMessage):
"""
Send the message via twilio
"""
try:
ph_number = twilio_message.phone_number
message = twilio_message.message
msg = TWILIO_CLIENT.messages.create(to=ph_number,
messaging_service_sid=TWILIO_MESSAGING_SERVICE_SID,
body=message)
except TwilioRestException as e:
raise
return msg, msg.sid
实际的服务器和 uvicorn 一样非常基本
sudo ~/.pyenv/versions/myproject/bin/uvicorn app:app --port xxxx --host 0.0.0.0 --ssl-keyfile=/etc/letsencrypt/live/myprojectdomain/privkey.pem --ssl-certfile=/etc/letsencrypt/live/myprojectdomain/fullchain.pem
您没有在问题中包含最重要的信息,即包含 SSL 错误的实际日志。所以我要做出一个最好的猜测。
Let's Encrypt 证书需要每三个月更新一次。我的假设是您有一个 cron 作业、计时器或类似的 运行s certbot
定期更新任何接近到期日期的证书。续订非常透明,fullchain.pem
文件刚刚更新到位。
因此,在最好的情况下,您需要每三个月重新启动一次 uvicorn
,以便重新加载证书。查看 certbot renew
命令的 --post-hook
选项:https://certbot.eff.org/docs/using.html?highlight=hook#renewing-certificates。这允许您在续订证书后添加自定义命令。您将使用它向您的 uvicorn 进程发送信号以重新加载或重新启动。
此外,这与您的问题无关,但您应该知道 Twilio 库在 asyncio 下阻塞,因此您应该 运行 在执行程序或其他方法中调用 Twilio API 的任何函数防止异步循环阻塞。我在 Twilio 博客上的 Using the Twilio Python Helper Library in your Async Applications 文章将为您提供一个选项列表。
我有一个基本的休息 api 服务器,它使用 Twilio api 接受传入的短信和回复。 每两个月左右我就注意到我需要重新启动服务器,因为 Twilio 开始给我 SSL 握手错误。这与 https 证书没有任何关系,因为正如我所提到的,它可以通过简单的重启来修复。我正在寻求调试帮助。
工具的结构
app.py - FastApi 休息服务器。有一个 /webhook 端点可以接受 Twilio 传入消息请求。
utils.py - 包含用于通过 Twilio 发送 SMS 的方法的 Utils 文件 api。
TWILIO_CLIENT = Client(TWILIO_SID, TWILIO_AUTH_TOKEN)
async def send_twilio_sms(twilio_message: TwilioMessage):
"""
Send the message via twilio
"""
try:
ph_number = twilio_message.phone_number
message = twilio_message.message
msg = TWILIO_CLIENT.messages.create(to=ph_number,
messaging_service_sid=TWILIO_MESSAGING_SERVICE_SID,
body=message)
except TwilioRestException as e:
raise
return msg, msg.sid
实际的服务器和 uvicorn 一样非常基本
sudo ~/.pyenv/versions/myproject/bin/uvicorn app:app --port xxxx --host 0.0.0.0 --ssl-keyfile=/etc/letsencrypt/live/myprojectdomain/privkey.pem --ssl-certfile=/etc/letsencrypt/live/myprojectdomain/fullchain.pem
您没有在问题中包含最重要的信息,即包含 SSL 错误的实际日志。所以我要做出一个最好的猜测。
Let's Encrypt 证书需要每三个月更新一次。我的假设是您有一个 cron 作业、计时器或类似的 运行s certbot
定期更新任何接近到期日期的证书。续订非常透明,fullchain.pem
文件刚刚更新到位。
因此,在最好的情况下,您需要每三个月重新启动一次 uvicorn
,以便重新加载证书。查看 certbot renew
命令的 --post-hook
选项:https://certbot.eff.org/docs/using.html?highlight=hook#renewing-certificates。这允许您在续订证书后添加自定义命令。您将使用它向您的 uvicorn 进程发送信号以重新加载或重新启动。
此外,这与您的问题无关,但您应该知道 Twilio 库在 asyncio 下阻塞,因此您应该 运行 在执行程序或其他方法中调用 Twilio API 的任何函数防止异步循环阻塞。我在 Twilio 博客上的 Using the Twilio Python Helper Library in your Async Applications 文章将为您提供一个选项列表。