SSL握手问题与Pymongo上Python3

问题描述 投票:1回答:3

试图连接到Azure的CosmosDB蒙戈服务器的测试结果为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://[email protected]: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}集(数据库(MongoClient(主机= [ 'backend.documents.azure.com:10255'],document_class =字典,tz_aware =假,连接=真,SSL =真,replicaset = 'globaldb'),u'result_DB '),u'file_result')

python python-3.x azure ssl azure-cosmosdb-mongoapi
3个回答
2
投票

解决了这一变化的问题:

client = pymongo.MongoClient(uri, ssl_cert_reqs=ssl.CERT_NONE)

1
投票

面对试图从mongodb的数字海洋,利用与MongoClient PARAMS此功能解决了连接时,同样的问题:

def get_client(host,port,username,password,db):
      return MongoClient('mongodb://{}:{}/'.format(host,port),
                         username=username,
                         password=password,
                         authSource=db,
                         ssl=True,ssl_cert_reqs=ssl.CERT_NONE)

client = get_client("host-ip","port","username","password","db-name")

0
投票

在PyMongo正式文件`TLS / SSL和PyMongo的部分Troubleshooting TLS Errors介绍这个问题如下。

TLS错误通常分为两类,证书验证失败或协议版本不匹配。类似如下的错误消息意味着,OpenSSL的是无法核实该服务器的证书:

[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

这往往是因为OpenSSL的不能访问到系统的根证书或证书已过期。 Linux用户应该确保他们从Linux厂商安装了最新的根证书更新。 MacOS的用户使用Python 3.6.0或更新从python.org may have to run a script included with python下载安装根证书:

open "/Applications/Python <YOUR PYTHON VERSION>/Install Certificates.command"

年长PyPy和PyPy3便携式版本的用户可能不得不set an environment variable告诉OpenSSL的在哪里找到根证书。这是很容易使用一封来自PyPI的certifi module完成:

$ pypy -m pip install certifi
$ export SSL_CERT_FILE=$(pypy -c "import certifi; print(certifi.where())")

您可以尝试按照上面的说明来解决你的问题,这似乎是Linux和Mac用户。在Windows上,我不能重现在Python 3.73.6您的问题。如果您有任何问题,请随时让我知道。

© www.soinside.com 2019 - 2024. All rights reserved.