Python3.9运行Telegram Bot遇SSL wrong_version_number错误求助
Python 3.9运行Telegram Bot遭遇SSL wrong_version_number异常
问题详情
我使用Python 3.9运行Telegram Bot时触发了SSL wrong_version_number异常。已尝试修改/Users/abc/Library/Python/3.9/lib/python/site-packages/telegram/utils/request.py中的证书验证配置为CERT_NONE,修改内容如下,但问题仍未解决:
kwargs = dict( maxsize=con_pool_size, cert_reqs='CERT_NONE', ca_certs=certifi.where(), socket_options=sockopts, timeout=urllib3.Timeout(connect=self._connect_timeout, read=read_timeout, total=None), )
主代码(main.py)
import logging from telegram.ext import * import responses API_KEY = 'my-api-key' # 配置日志 logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logging.info('Starting Bot...') def start_command(update, context): update.message.reply_text('Hello there! I\'m a bot. What\'s up?') def help_command(update, context): update.message.reply_text('Try typing anything and I will do my best to respond!') def custom_command(update, context): update.message.reply_text('This is a custom command, you can add whatever text you want here.') def handle_message(update, context): text = str(update.message.text).lower() logging.info(f'User ({update.message.chat.id}) says: {text}') # 生成机器人回复 response = responses.get_response(text) update.message.reply_text(response) def error(update, context): # 记录错误日志 logging.error(f'Update {update} caused error {context.error}') # 运行程序 if __name__ == '__main__': updater = Updater(API_KEY, use_context=True) dp = updater.dispatcher # 注册命令处理器 dp.add_handler(CommandHandler('start', start_command)) dp.add_handler(CommandHandler('help', help_command)) dp.add_handler(CommandHandler('custom', custom_command)) # 注册消息处理器 dp.add_handler(MessageHandler(Filters.text, handle_message)) # 注册错误处理器 dp.add_error_handler(error) # 启动机器人轮询 updater.start_polling(1.0) updater.idle()
完整错误堆栈
Traceback (most recent call last): File "/Users/abc/Documents/professional/projects/telegram-bot/main.py", line 55, in <module> updater.start_polling(1.0) File "/Users/abc/Library/Python/3.9/lib/python/site-packages/telegram/ext/updater.py", line 422, in start_polling self._init_thread(self.dispatcher.start, "dispatcher", ready=dispatcher_ready) File "/Users/abc/Library/Python/3.9/lib/python/site-packages/telegram/ext/updater.py", line 342, in _init_thread name=f"Bot:{self.bot.id}:{name}", File "/Users/abc/Library/Python/3.9/lib/python/site-packages/telegram/bot.py", line 372, in id return self.bot.id File "/Users/abc/Library/Python/3.9/lib/python/site-packages/telegram/bot.py", line 366, in bot self._bot = self.get_me() File "/Users/abc/Library/Python/3.9/lib/python/site-packages/telegram/bot.py", line 133, in decorator result = func(*args, **kwargs) File "/Users/abc/Library/Python/3.9/lib/python/site-packages/telegram/bot.py", line 453, in get_me result = self._post('getMe', timeout=timeout, api_kwargs=api_kwargs) File "/Users/abc/Library/Python/3.9/lib/python/site-packages/telegram/bot.py", line 298, in _post return self.request.post( File "/Users/abc/Library/Python/3.9/lib/python/site-packages/telegram/utils/request.py", line 361, in post result = self._request_wrapper( File "/Users/abc/Library/Python/3.9/lib/python/site-packages/telegram/utils/request.py", line 265, in _request_wrapper raise NetworkError(f'urllib3 HTTPError {error}') from error telegram.error.NetworkError: urllib3 HTTPError [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1129)
解决建议
- 排查代理冲突:该错误多由代理环境不兼容TLS版本导致。若使用了代理,先关闭代理测试;若必须使用代理,确认代理支持TLS 1.2及以上版本,或调整代理的SSL配置。
- 更新依赖库:旧版本的
python-telegram-bot、urllib3可能存在SSL兼容问题,执行以下命令更新:
pip install --upgrade python-telegram-bot urllib3 certifi
- 自定义Request对象:不要修改库源码,在自己的代码中强制指定SSL版本,示例如下:
from telegram.utils.request import Request import ssl # 初始化自定义Request,指定TLS版本 request = Request( con_pool_size=8, ssl_context=ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) ) # 用自定义Request创建Updater updater = Updater(API_KEY, request=request, use_context=True)
- 修复系统SSL环境:macOS下Python的SSL库可能与系统环境冲突,尝试用pyenv重新安装Python 3.9,避免系统自带SSL干扰。
内容的提问来源于stack exchange,提问作者beskar
相关产品推荐
相关产品推荐

