tencent cloud

フィードバック

Python Connection Sample

最終更新日:2024-11-05 10:10:15
    This document provides client code samples for Python to help you access a database with or without SSL encryption enabled.

    Prerequisites

    Get the private IPv4 address and port information for database connection in the Network Info section on the Instance Details page in the TencentDB for Redis® console. For detailed directions, see Viewing Instance Details.
    Get the account and password for database access. For detailed directions, see Managing Account.
    Download and install redis-py. The latest version is recommended.
    If you want to connect to the database over SSL, enable SSL encryption to get the SSL certificate file.

    Connection Sample Without SSL Encryption Enabled

    You need to modify the parameters based on the comments, including IP, port, account, and password for database access.
    #!/usr/bin/env python3
    #-*- coding: utf-8 -*-
    import redis
    
    #Replace with the connected instance host and port here
    host = '192.xx.xx.195'
    port = 6379
    
    #Replace with the instance ID and password here
    user='username'
    pwd='password'
    
    #When connecting, specify the AUTH information through the `password` parameter. If you connect through the default account, it is `pwd`. If you connect through a custom account, it is `user+'@'+pwd`
    r = redis.StrictRedis(host=host, port=port, password=user+'@'+pwd)
    
    #Database operations can be performed after the connection is established. For more information, visit https://github.com/andymccurdy/redis-py
    r.set('name', 'python_test');
    print r.get('name')
    Execution result:
    img
    
    

    Connection Sample With SSL Encryption Enabled

    You need to modify the parameters based on the comments, including SSL certificate file, IP, port, account, and password for database access.
    import redis3 as redis3
    
    if __name__ == "__main__":
    #`vip` is the private IPv4 address for database connection, `6379` is the default port number, `pwd` is the password of the default account, and `ca.pem` is the obtained SSL certificate file. You need to replace them as needed.
    client = redis3.Redis(host="vip", port=6379, password="pwd", ssl=True, ssl_cert_reqs="required",
    ssl_ca_certs="ca.pem")
    print(client.ping())
    お問い合わせ

    カスタマーサービスをご提供できるため、ぜひお気軽にお問い合わせくださいませ。

    テクニカルサポート

    さらにサポートが必要な場合は、サポートチケットを送信して弊社サポートチームにお問い合わせください。24時間365日のサポートをご提供します。

    電話サポート(24 時間365日対応)