gpt4 book ai didi

python - Python3 上 Pymongo 的 SSL 握手问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:34:01 25 4
gpt4 key购买 nike

尝试连接到 Azure CosmosDB mongo 服务器会导致 SSL 握手错误。

我正在使用 Python3Pymongo 连接到我的 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://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5eee0fc949796e5c7c4c6cec0cbc18bc1cac6d0c8c0cbd1d68bc4dfd0d7c08bc6cac8" rel="noreferrer noopener nofollow">[email protected]</a>: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 handshake failed: backendstore.documents.azure.com:10255: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

这是我使用 Python27 运行相同代码时的成功输出:

Database server status: { "_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')

最佳答案

在 Windows 上你可以这样做

pip install certifi

然后在代码中使用它:

import certifi
ca = certifi.where()

client = pymongo.MongoClient(
"mongodb+srv://username:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e292839191958d9086a2818e9791968790d2cc9a9a9a9a9acc8f8d8c858d8680cc8c8796" rel="noreferrer noopener nofollow">[email protected]</a>/xyzdb?retryWrites=true&w=majority", tlsCAFile=ca)

关于python - Python3 上 Pymongo 的 SSL 握手问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54484890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com