gpt4 book ai didi

python - pymongo 在尝试连接到本地 Mongo 实例时返回 SSL 错误

转载 作者:行者123 更新时间:2023-12-04 22:43:08 25 4
gpt4 key购买 nike

我正在编写一个小型应用程序,它将 JSON 文件写入 Mongo 集合,然后根据给定的输入检索它们。我将 Mongo 设置存储为 Pydantic 模型,如下所示 settings.py :

from typing import Optional
from pydantic import BaseSettings

__all__ = ["mongo_settings"]


class MongoSettings(BaseSettings):
host: str = "mongodb://127.0.0.1"
port: int = 27017
minPoolSize: int = 10
maxPoolSize: int = 10
username: str = None
password: str = None
authSource: Optional[str] = ""
tls: bool = True
tlsAllowInvalidCertificates: bool = True

mongo_settings = MongoSettings()
这些用于 Database具有返回 MongoClient 的方法的类:
from pymongo import MongoClient
from .settings import mongo_settings


class Database:
client: MongoClient = None

def get_database() -> MongoClient:
client = MongoClient(**mongo_settings.dict())
return client
我编写了一个(非常)基本的测试来检查我的应用程序是否可以连接到本地运行的 Mongo 实例:
from pymongo import MongoClient
from database.db import Database

def test_database_connection():

mongo: MongoClient = Database.get_database()

mongo.server_info()
但是,这会失败并出现以下错误:
Traceback (most recent call last):
File "/code/project-env/lib/python3.8/site-packages/pymongo/pool.py", line 909, in _configured_socket
sock = ssl_context.wrap_socket(sock, server_hostname=host)
File "/home/.pyenv/versions/3.8.2/lib/python3.8/ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "/home/.pyenv/versions/3.8.2/lib/python3.8/ssl.py", line 1040, in _create
self.do_handshake()
File "/home/.pyenv/versions/3.8.2/lib/python3.8/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:1108)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/code/project-env/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1749, in _process_periodic_tasks
self._topology.update_pool()
File "/code/project-env/lib/python3.8/site-packages/pymongo/topology.py", line 438, in update_pool
server._pool.remove_stale_sockets(pool_id)
File "/code/project-env/lib/python3.8/site-packages/pymongo/pool.py", line 1060, in remove_stale_sockets
sock_info = self.connect()
File "/code/project-env/lib/python3.8/site-packages/pymongo/pool.py", line 1089, in connect
sock = _configured_socket(self.address, self.opts)
File "/code/project-env/lib/python3.8/site-packages/pymongo/pool.py", line 922, in _configured_socket
_raise_connection_failure(address, exc, "SSL handshake failed: ")
File "/code/project-env/lib/python3.8/site-packages/pymongo/pool.py", line 290, in _raise_connection_failure
raise AutoReconnect(msg)
pymongo.errors.AutoReconnect: SSL handshake failed: 127.0.0.1:27017: EOF occurred in violation of protocol (_ssl.c:1108)
我试过玩 /etc/mongod.conf文件以查看是否可以解决问题,使用 net block 目前看起来像这样:
net:
port: 27017
ssl:
mode: requireSSL
CAFile: "~/.ssl/ca.pem"
PEMKeyFile: "~/.ssl/server.pem"
PEMKeyPassword: 'XXXXXXXXX'
allowConnectionsWithoutCertificates: true
但这并没有产生任何结果。我觉得我在这里遗漏了一些明显的东西,导致它不起作用,有人知道我做错了什么吗?

最佳答案

事实证明代码本身没有任何问题——mongod.conf 中的波浪号快捷方式文件意味着证书没有被正确读取。将其换成绝对路径似乎已经解决了问题👍

关于python - pymongo 在尝试连接到本地 Mongo 实例时返回 SSL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63174839/

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