尝试连接到 Azure CosmosDB mongo 服务器会导致 SSL 握手错误。
我正在使用Python3
并Pymongo
连接到我的 Azure CosmosDB。如果我使用 Python27 运行代码,则连接工作正常,但在使用 Python3 时会导致以下错误:
import pymongo
from pymongo import MongoClient
import json
import sys
def check_server_status(client, data):
'''check the server status of the connected endpoint'''
db = client.result_DB
server_status = db.command('serverStatus')
print('Database server status:')
print(json.dumps(server_status, sort_keys=False, indent=2, separators=(',', ': ')))
coll = db.file_result
print (coll)
coll.insert_one(data)
def main():
uri = "mongodb://KEY123@backend.documents.azure.com:10255/?ssl=true&replicaSet=globaldb"
client = pymongo.MongoClient(uri)
emp_rec1 = {
"name":"Mr.Geek",
"eid":24,
"location":"delhi"
}
check_server_status(client, emp_rec1)
if __name__ == "__main__":
main()
运行它Python3
会导致以下错误:
pymongo.errors.ServerSelectionTimeoutError: SSL 握手失败: backendstore.documents.azure.com:10255: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败 (_ssl.c:749)
这是我运行相同代码时的成功输出Python27
:
数据库服务器状态: { "_t": "OKMongoResponse", "ok": 1 } Collection(Database(MongoClient(host=['backend.documents.azure.com:10255'], document_class=dict, tz_aware=False, connect =True, ssl=True, replicaset='globaldb'), u'result_DB'), u'file_result')