代码之家  ›  专栏  ›  技术社区  ›  Siddharth Kumar

Pymongo:SSL握手失败:EOF违反协议(\u SSL.c:590)

  •  0
  • Siddharth Kumar  · 技术社区  · 7 年前

    Traceback (most recent call last):
      File "pymongo_ssl.py", line 7, in <module>
        print mongoClient.database_names()
      File "/home/modak/.virtualenvs/enod-venv/local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 1149, in database_names
        "listDatabases")["databases"]]
      File "/home/modak/.virtualenvs/enod-venv/local/lib/python2.7/site-packages/pymongo/database.py", line 491, in command
        with client._socket_for_reads(read_preference) as (sock_info, slave_ok):
      File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
        return self.gen.next()
      File "/home/modak/.virtualenvs/enod-venv/local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 859, in _socket_for_reads
        with self._get_socket(read_preference) as sock_info:
      File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
        return self.gen.next()
      File "/home/modak/.virtualenvs/enod-venv/local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 823, in _get_socket
        server = self._get_topology().select_server(selector)
      File "/home/modak/.virtualenvs/enod-venv/local/lib/python2.7/site-packages/pymongo/topology.py", line 214, in select_server
        address))
      File "/home/modak/.virtualenvs/enod-venv/local/lib/python2.7/site-packages/pymongo/topology.py", line 189, in select_servers
        self._error_message(selector))
    pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed: EOF occurred in violation of protocol (_ssl.c:590)
    

    Python代码

    from pymongo import MongoClient
    MONGO_URI='mongodb://localhost:27017'
    mongoClient = MongoClient(MONGO_URI, ssl=True, ssl_ca_certs='ca-certs.pem')
    print mongoClient.database_names()`
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   qff    6 年前

    您可能有一个只支持TLS 1.0而不支持TLS 1.1或更高版本的Python设置。这至少是我遇到的问题。

    您可以这样检查:

    > from urllib.request import urlopen
    > urlopen('https://www.howsmyssl.com/a/check').read()
    

    Python 2

    > from urllib2 import urlopen
    > urlopen('https://www.howsmyssl.com/a/check').read()
    

    检查钥匙的输出 tls_version . 如果它说 TLS 1.0 而不是 TLS 1.1 TLS 1.2 这可能就是问题所在。

    如果您使用的是virtualenv,请确保在内部运行该命令。

    解决方案:使用更新版本的OpenSSL安装Python

    OS X .

    virtualenv用户

        2
  •  1
  •   Kasper747    3 年前

    试试这样的。在mongoengine为我工作:

    ent#preReq:  pip3 install certifi
    from pymongo import MongoClient
    import pymongo
    import certifi
    
    client = pymongo.MongoClient(URI, tlsCAFile=certifi.where())