You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pyrogram库run方法在Ubuntu服务器执行无响应求助

解决方案:Pyrogram run方法在Ubuntu 18.04挂起无响应的排查与修复

1. 强制开启调试日志定位卡点

无日志输出根本没法判断问题,先给Pyrogram加上详细日志:

import logging
from pyrogram import Client

logging.basicConfig(level=logging.DEBUG)

app = Client("my_account")
app.run()

运行后盯着终端,重点看Connection、Auth相关的日志,确定是连接阶段还是认证环节卡住。

2. 更新系统CA证书

Ubuntu 18.04的根证书可能过期,导致Pyrogram无法验证Telegram服务器的SSL证书,直接卡住。执行命令更新:

sudo apt-get install --reinstall ca-certificates
sudo update-ca-certificates -f

更新完再跑代码试试。

3. 手动验证Telegram连通性

先排除网络防火墙或路由问题,测试服务器到Telegram API的连通性:

# 测试主API服务器
curl -v https://api.telegram.org
# 测试备用服务器
curl -v https://api.core.telegram.org

如果curl返回SSL错误或超时,去检查服务器的出站防火墙规则,确保443端口能访问Telegram的IP段。

4. 临时禁用TgCrypto排查兼容问题

虽然装了TgCrypto,但可能和当前Python/系统环境不兼容。先禁用它测试:

from pyrogram import Client

app = Client("my_account", use_crypto=False)
app.run()

如果能正常启动,说明是TgCrypto的问题,重新编译安装:

pip uninstall -y tgcrypto
pip install tgcrypto --no-binary :all:

5. 检查依赖版本冲突

源码装的Python可能存在依赖路径问题,先查关键依赖版本:

python3.10 -m pip list | grep -E "pyrogram|tgcrypto|aiohttp"

确保aiohttp版本在3.8.x以上,过低版本会导致异步IO挂起,版本不够就更新:

python3.10 -m pip install --upgrade aiohttp

6. 用最小化代码测试

排除业务代码干扰,写个极简版Pyrogram代码测试:

from pyrogram import Client

# 替换成你的API ID和Hash
app = Client("test_account", api_id=12345, api_hash="your_api_hash")

@app.on_message()
async def hello(client, message):
    await message.reply("Hello from Pyrogram!")

app.run()

还是挂起的话,手动跑异步初始化流程:

import asyncio
from pyrogram import Client

async def main():
    app = Client("test_account", api_id=12345, api_hash="your_api_hash")
    await app.start()
    print("Client started successfully")
    await app.stop()

asyncio.run(main())

看能不能打印成功信息,判断是run方法的异步循环问题,还是初始化阶段就卡住了。


内容的提问来源于stack exchange,提问作者Dron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 02:22:43