Home >Database >Redis >How to catch redis exception in Python

How to catch redis exception in Python

WBOY
WBOYforward
2023-06-02 23:14:46727browse

Python captures redis exception

Scenario recurrence

Use python's redis package to connect to redis and deliberately set a wrong password, but found that it did not raise an exception

Environment

System: win 10

Python version: 3.6.8

Initial code

import redis
host = "127.0.0.1"
port = 6379
password = "123456"
redis_conn = redis.Redis(host=host,port=port,password)

After testing, we found that after creating the connection object, we When querying through the conn object, it threw a password error exception

Capture the redis connection exception code

import redis
host = "127.0.0.1"
port = 6379
password = "123456"
try:
    redis_conn = redis.Redis(host=host,port=port,password)
    # 在创建连接后执行一个查询操作
    redis_conn.client_list()
except redis.ConnectError as err:
    print(str(err))

The above is the detailed content of How to catch redis exception in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete