簡體   English   中英

如何在Macbook Air上通過Python 3.6連接到MongoDB Atlas,而不會發生SSL握手錯誤?

[英]How can I connect to MongoDB Atlas through Python 3.6 on my Macbook Air without SSL handshake error?

我已經建立了一個測試MongoDb Atlas帳戶,這只是一個免費帳戶,並在Ubuntu盒子上使用以下代碼,我可以成功創建用戶並進行搜索。

當我在MacBook Air上嘗試完全相同的操作時,會得到如下所示的SSL握手。

pymongo.errors.ServerSelectionTimeoutError:SSL握手失敗:[SSL:CERTIFICATE_VERIFY_FAILED]證書驗證失敗(_ssl.c:833),SSL握手失敗:[SSL:CERTIFICATE_VERIFY_FAILED]證書驗證失敗(_ssl.c:833),SSL握手失敗: [SSL:CERTIFICATE_VERIFY_FAILED]證書驗證失敗(_ssl.c:833)

import pymongo
client = pymongo.MongoClient("mongodb+srv://MYUSERNAME:MYPASSWORD@cluster0-ABCDEF.mongodb.net/test")
db = client.johnny
collection = db.myjohnnytest

example = {'name' : 'Johnny',
            'email' : 'johnny@test.net'}

user_id = collection.insert_one(example).inserted_id

我發現這個答案似乎很關鍵:

堆棧溢出答案

但是我此后嘗試了此操作,並且無論是嚴格按照它進行操作還是激活我的virtualenv,我都會收到以下錯誤消息:

找不到激活的virtualenv(必需)。 追溯(最近一次通話最近):主文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py”中的文件“”,行44,在文件“”,行25,第291行,在check_call中引發CalledProcessError(retcode,cmd)子進程。CalledProcessError:命令'['/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6'、'-E'、'-s' ,'-m','pip','install','-upgrade','certifi']'返回非零退出狀態3.注銷保存會話... ...復制共享歷史記錄.....正在保存歷史記錄...截斷歷史記錄文件...已完成。

默認情況下,PyMongo配置為在啟用TLS時要求服務器提供證書。 可以使用ssl_cert_reqs選項進行配置。 要禁用此要求,請將ssl.CERT_NONE作為關鍵字參數傳遞:

>>> uri = 'mongodb://example.com/?ssl=true&ssl_cert_reqs=CERT_NONE'
>>> client = pymongo.MongoClient(uri)

我展示了兩種解決方法:

  1. (最簡單)

...&ssl=true&ssl_ca_certs=/path/to/cert.pem到mongodb url

  1. (推薦的)

配置python SSL連接。

$ python3 -c "import ssl; print(ssl.get_default_verify_paths())
DefaultVerifyPaths(cafile='/Library/Frameworks/Python.framework/Versions/3.6/etc/openssl/cert.pem', capath=None, openssl_cafile_env='SSL_CERT_FILE', openssl_cafile='/Library/Frameworks/Python.framework/Versions/3.6/etc/openssl/cert.pem', openssl_capath_env='SSL_CERT_DIR', openssl_capath='/Library/Frameworks/Python.framework/Versions/3.6/etc/openssl/certs')

然后確保將cert.pem文件添加到指定的路徑。 就我而言,我應該將其添加到/Library/Frameworks/Python.framework/Versions/3.6/etc/openssl/

您是否更新了適用於Python的CA捆綁軟件?

從bash shell /終端:打開“ / Applications / Python 3.6 / Install Certificates.command”

或Python 3.7等

MacOS,Python和CA上 有關Python Stackoverflow的 MongoDB文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM