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

How to catch redis exception in Python

WBOY
Release: 2023-06-02 23:14:46
forward
747 people have browsed it

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)
Copy after login

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))
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template