Comprehensive Python SDK for integrating with Tencent Cloud services, supporting 240+ cloud services with authentication, error handling, and retry mechanisms.
Comprehensive configuration system for HTTP settings, retry behavior, circuit breakers, request customization, and regional failover. The profile system allows fine-grained control over SDK behavior across different deployment environments.
Master configuration object that controls SDK-wide behavior including authentication signatures, internationalization, retry policies, and circuit breaker settings.
class ClientProfile:
def __init__(self, signMethod: str = "TC3-HMAC-SHA256", httpProfile = None, language: str = "zh-CN", debug: bool = False):
"""
Initialize client configuration profile.
Parameters:
- signMethod (str): API signature algorithm ("TC3-HMAC-SHA256", "HmacSHA1", "HmacSHA256")
- httpProfile (HttpProfile, optional): HTTP-specific configuration
- language (str): Response language ("zh-CN", "en-US")
- debug (bool): Enable debug mode for detailed logging
"""
# Configuration properties
self.signMethod: str = signMethod
self.httpProfile = httpProfile
self.language: str = language
self.debug: bool = debug
self.retryer = None # StandardRetryer or NoopRetryer
self.disable_region_breaker: bool = True
self.region_breaker_profile = None # RegionBreakerProfileBasic Configuration:
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.cvm.v20170312 import cvm_client
from tencentcloud.common import credential
# Create basic configuration
client_profile = ClientProfile()
client_profile.signMethod = "TC3-HMAC-SHA256" # Default, most secure
client_profile.language = "en-US" # English responses
client_profile.debug = True # Enable detailed logging
# Use with client
cred = credential.DefaultCredentialProvider().get_credential()
client = cvm_client.CvmClient(cred, "ap-shanghai", client_profile)Detailed HTTP-level configuration including protocols, endpoints, timeouts, keep-alive settings, proxy configuration, and SSL certificate handling.
class HttpProfile:
def __init__(self, protocol: str = "https", endpoint: str = None, reqMethod: str = "POST", reqTimeout: int = 60):
"""
Initialize HTTP configuration profile.
Parameters:
- protocol (str): Connection protocol ("https", "http")
- endpoint (str, optional): Custom service endpoint
- reqMethod (str): HTTP method ("POST", "GET")
- reqTimeout (int): Request timeout in seconds (default: 60)
"""
# HTTP configuration properties
self.protocol: str = protocol
self.endpoint: str = endpoint
self.reqMethod: str = reqMethod
self.reqTimeout: int = reqTimeout
self.keepAlive: bool = False
self.proxy: str = None
self.certification = None # SSL certificate path or False to skipHTTP Configuration Examples:
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.profile.client_profile import ClientProfile
# Basic HTTP configuration
http_profile = HttpProfile()
http_profile.protocol = "https" # Use HTTPS (recommended)
http_profile.reqMethod = "POST" # Default for most APIs
http_profile.reqTimeout = 30 # 30-second timeout
http_profile.keepAlive = True # Enable connection reuse
# Custom endpoint (for specific regions or testing)
http_profile.endpoint = "cvm.ap-shanghai.tencentcloudapi.com"
# Attach to client profile
client_profile = ClientProfile()
client_profile.httpProfile = http_profileProxy Configuration:
# HTTP proxy
http_profile = HttpProfile()
http_profile.proxy = "http://proxy-server:8080"
# Authenticated proxy
http_profile.proxy = "http://username:password@proxy-server:8080"
# HTTPS proxy
http_profile.proxy = "https://secure-proxy:8443"SSL Certificate Configuration:
# Custom certificate file
http_profile = HttpProfile()
http_profile.certification = "/path/to/certificate.pem"
# Skip certificate verification (not recommended for production)
http_profile.certification = False
# Use system certificates (default)
http_profile.certification = NoneAutomatic failover to backup regions when primary endpoints fail, with configurable circuit breaker patterns to handle transient failures gracefully.
class RegionBreakerProfile:
def __init__(self, backup_endpoint: str, max_fail_num: int = 5, max_fail_percent: float = 0.75, window_interval: int = 300, timeout: int = 60, max_requests: int = 5):
"""
Initialize region circuit breaker configuration.
Parameters:
- backup_endpoint (str): Fallback region endpoint (format: {region}.tencentcloudapi.com)
- max_fail_num (int): Maximum failures before circuit opens (default: 5)
- max_fail_percent (float): Maximum failure percentage (0.0-1.0, default: 0.75)
- window_interval (int): Failure counting window in seconds (default: 300)
- timeout (int): Circuit open duration in seconds (default: 60)
- max_requests (int): Max successful requests to close circuit (default: 5)
"""Circuit Breaker States:
Circuit Breaker Configuration:
from tencentcloud.common.profile.client_profile import ClientProfile, RegionBreakerProfile
# Simple circuit breaker setup
client_profile = ClientProfile()
client_profile.disable_region_breaker = False # Enable circuit breaker
# Advanced circuit breaker configuration
region_breaker = RegionBreakerProfile(
backup_endpoint="ap-beijing.tencentcloudapi.com", # Backup region
max_fail_num=3, # Open after 3 failures
max_fail_percent=0.5, # Or 50% failure rate
window_interval=60, # 1-minute failure window
timeout=30, # Stay open for 30 seconds
max_requests=3 # Close after 3 successful requests
)
client_profile.region_breaker_profile = region_breaker
# Use with client
client = cvm_client.CvmClient(cred, "ap-shanghai", client_profile)Development Environment Configuration:
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
import logging
# Development-friendly configuration
http_profile = HttpProfile()
http_profile.reqTimeout = 10 # Short timeout for quick feedback
http_profile.keepAlive = False # Disable keep-alive for testing
client_profile = ClientProfile()
client_profile.httpProfile = http_profile
client_profile.debug = True # Enable debug logging
client_profile.language = "en-US" # English error messages
# Configure client with dev settings
client = cvm_client.CvmClient(cred, "ap-shanghai", client_profile)
client.set_stream_logger(sys.stdout, logging.DEBUG)Production Environment Configuration:
from tencentcloud.common.profile.client_profile import ClientProfile, RegionBreakerProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common import retry
import logging
# Production HTTP configuration
http_profile = HttpProfile()
http_profile.keepAlive = True # Connection reuse
http_profile.reqTimeout = 60 # Longer timeout for stability
http_profile.certification = "/etc/ssl/certs/ca-certificates.crt" # Explicit cert
# Production retry configuration
logger = logging.getLogger("tencentcloud.retry")
logger.setLevel(logging.WARNING) # Only log warnings/errors
retryer = retry.StandardRetryer(max_attempts=5, logger=logger)
# Production circuit breaker
region_breaker = RegionBreakerProfile(
backup_endpoint="ap-beijing.tencentcloudapi.com",
max_fail_num=5,
max_fail_percent=0.75,
window_interval=300,
timeout=60,
max_requests=5
)
# Combine all production settings
client_profile = ClientProfile()
client_profile.httpProfile = http_profile
client_profile.retryer = retryer
client_profile.disable_region_breaker = False
client_profile.region_breaker_profile = region_breaker
client_profile.language = "en-US"
client_profile.debug = False
client = cvm_client.CvmClient(cred, "ap-shanghai", client_profile)High-Performance Configuration:
# Optimized for high-throughput scenarios
http_profile = HttpProfile()
http_profile.keepAlive = True # Reuse connections
http_profile.reqTimeout = 30 # Balance speed vs reliability
http_profile.reqMethod = "GET" # GET requests for read operations
client_profile = ClientProfile()
client_profile.httpProfile = http_profile
client_profile.debug = False # Disable debug overhead
client_profile.retryer = retry.NoopRetryer() # No automatic retries
client = cvm_client.CvmClient(cred, "ap-shanghai", client_profile)Multi-Region Configuration:
def create_multi_region_clients(regions, base_profile=None):
"""Create optimized clients for multiple regions."""
clients = {}
for region in regions:
# Clone base profile or create new one
profile = base_profile or ClientProfile()
# Region-specific HTTP settings
http_profile = HttpProfile()
http_profile.endpoint = f"cvm.{region}.tencentcloudapi.com"
http_profile.keepAlive = True
profile.httpProfile = http_profile
# Create region-specific client
clients[region] = cvm_client.CvmClient(cred, region, profile)
return clients
# Usage
regions = ["ap-shanghai", "ap-beijing", "ap-guangzhou"]
clients = create_multi_region_clients(regions)
# Use region-specific clients
for region, client in clients.items():
response = client.DescribeInstances(models.DescribeInstancesRequest())
print(f"{region}: {response.TotalCount} instances")Configuration Factory Pattern:
from tencentcloud.common.profile.client_profile import ClientProfile, RegionBreakerProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common import retry
import os
class TencentCloudConfig:
@staticmethod
def development():
"""Development environment configuration."""
http_profile = HttpProfile()
http_profile.reqTimeout = 10
client_profile = ClientProfile()
client_profile.httpProfile = http_profile
client_profile.debug = True
client_profile.language = "en-US"
return client_profile
@staticmethod
def production():
"""Production environment configuration."""
http_profile = HttpProfile()
http_profile.keepAlive = True
http_profile.reqTimeout = 60
region_breaker = RegionBreakerProfile(
backup_endpoint="ap-beijing.tencentcloudapi.com"
)
client_profile = ClientProfile()
client_profile.httpProfile = http_profile
client_profile.disable_region_breaker = False
client_profile.region_breaker_profile = region_breaker
client_profile.retryer = retry.StandardRetryer(max_attempts=3)
return client_profile
@staticmethod
def from_environment():
"""Load configuration from environment variables."""
env = os.getenv("TENCENTCLOUD_ENV", "development")
if env == "production":
return TencentCloudConfig.production()
else:
return TencentCloudConfig.development()
# Usage
profile = TencentCloudConfig.from_environment()
client = cvm_client.CvmClient(cred, "ap-shanghai", profile)Install with Tessl CLI
npx tessl i tessl/pypi-tencentcloud-sdk-python