Linux上Docker部署的Cosmos emulator能否与Python程序交互?
问题原因说明
Linux版Cosmos emulator完全支持SQL API及Python SDK调用,你遇到的报错是SSL证书校验失败导致的,和API支持性无关。
Windows环境下安装模拟器时,安装程序会自动将模拟器的自签名SSL证书导入系统信任列表,因此代码运行不需要额外配置;而Linux环境通过Docker部署的模拟器不会自动完成证书信任配置,Python发起HTTPS请求时无法校验自签名证书合法性,就会抛出当前报错。
解决方案
方案1:临时关闭SSL校验(仅推荐本地测试使用,生产环境禁止使用)
创建CosmosClient时新增connection_verify=False参数,跳过SSL证书校验即可,修改后代码如下:
from azure.cosmos import exceptions, CosmosClient, PartitionKey endpoint = "https://localhost:8081" key = 'C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==' # 新增connection_verify参数关闭SSL校验 client = CosmosClient(endpoint, key, connection_verify=False)
方案2:配置证书信任(推荐)
- 首先导出Cosmos emulator的自签名证书,执行以下命令:
openssl s_client -connect localhost:8081 -showcerts < /dev/null | sed -n '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/p' > cosmos_emulator.crt
- 可选操作1:将证书导入Linux系统信任列表,以Debian/Ubuntu发行版为例:
sudo cp cosmos_emulator.crt /usr/local/share/ca-certificates/ sudo update-ca-certificates
配置完成后直接运行原有代码即可正常连接。
3. 可选操作2:不在系统层面导入证书,直接在代码中指定证书路径:
client = CosmosClient(endpoint, key, connection_verify="/path/to/cosmos_emulator.crt")
内容的提问来源于stack exchange,提问作者Millie Woo
相关产品推荐
相关产品推荐

