升级Python stomp至8.0.1后遇SSL库未找到错误,求解决方案
stomp.py升级后SSL连接失败问题
问题背景
将Python stomp客户端从7.0.0版本升级至8.0.1版本后,连接队列时出现以下错误:
Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/mpsmqutils/mqlistener.py", line 64, in connect_and_subscribe conn.set_ssl([(_host, _port)]) File "/usr/local/lib/python3.8/site-packages/stomp/connect.py", line 68, in set_ssl self.transport.set_ssl(*args, **kwargs) File "/usr/local/lib/python3.8/site-packages/stomp/transport.py", line 859, in set_ssl raise Exception("SSL connection requested, but SSL library not found") Exception: SSL connection requested, but SSL library not found
该错误来自stomp.py的transport.py文件中SSL检查逻辑,是版本更新时调整SSL依赖导致的。
核心问题
- 客户端连接代码需要做哪些修改才能恢复正常?
- 是否需要向
set_ssl()传入额外参数?
现有连接代码
def connect_and_subscribe(conn, queue=_queue, sub_id=_sub_id): print(_hostname_prefix + "************************ MQUTILS MQLISTENER - CONNECT_AND_SUBSCRIBE *******************************") global _reconnect_attempts _reconnect_attempts = _reconnect_attempts + 1 if _reconnect_attempts <= _max_attempts: # TODO: Retry timer with exponential backoff time.sleep(1) try: if not os.getenv('MQ_DISABLE_SSL'): if _failback_host and _failback_port: conn.set_ssl([(_host, _port),(_failback_host, _failback_port)]) else: conn.set_ssl([(_host, _port)]) if not conn.is_connected(): conn.connect(_user, _password, headers={'client-id': _client_id}, wait=True) print(f'{_hostname_prefix}connect_and_subscribe connecting {queue} to with connection id {sub_id} reconnect attempts: {_reconnect_attempts}', flush=True) else: print(f'{_hostname_prefix}connect_and_subscibe already connected {queue} to with connection id {sub_id} reconnect attempts {_reconnect_attempts}', flush=True) except Exception as e: print(_hostname_prefix + 'Exception on disconnect. reconnecting...') print(_hostname_prefix + traceback.format_exc()) connect_and_subscribe(conn) else: conn.subscribe(destination=queue, id=sub_id, ack='client-individual') _reconnect_attempts = 0 else: print('{}Maximum reconnect attempts reached for this connection. reconnect attempts: {}'.format(_hostname_prefix, _reconnect_attempts), flush=True)
解决方案
问题1:代码修改要点
stomp.py 8.x版本后,SSL依赖不再默认包含在基础安装包中,需先安装额外的SSL依赖库:
- 执行安装命令:
pip install stomp.py[ssl]
代码本身无需大幅修改,但要确保初始化连接时使用的是支持SSL的Transport类。新版本中,只要安装了ssl依赖,原有Connection类会自动启用SSL支持,无需手动替换。
问题2:是否需要额外参数
不需要向set_ssl()传入额外参数,原有的参数格式是正确的。但有两点需要注意:
- 必须先安装
stomp.py[ssl]依赖,否则即使参数正确,依然会抛出SSL库未找到的错误。 - 如果需要自定义SSL配置(如指定加密协议、证书文件等),可以在
set_ssl()中传入对应参数,示例:import ssl conn.set_ssl([(_host, _port)], ssl_version=ssl.PROTOCOL_TLSv1_2, cert_file="/path/to/cert.pem")
内容的提问来源于stack exchange,提问作者pengz
相关产品推荐
相关产品推荐

