MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / _connect

Method _connect

test/__init__.py:174–195  ·  view source on GitHub ↗
(self, host, port, **kwargs)

Source from the content-addressed store, hash-verified

172 return self._hello
173
174 def _connect(self, host, port, **kwargs):
175 kwargs.update(self.default_client_options)
176 client: MongoClient = pymongo.MongoClient(
177 host, port, serverSelectionTimeoutMS=5000, **kwargs
178 )
179 try:
180 try:
181 client.admin.command("ping") # Can we connect?
182 except pymongo.errors.OperationFailure as exc:
183 # SERVER-32063
184 self.connection_attempts.append(
185 f"connected client {client!r}, but legacy hello failed: {exc}"
186 )
187 else:
188 self.connection_attempts.append(f"successfully connected client {client!r}")
189 # If connected, then return client with default timeout
190 return pymongo.MongoClient(host, port, **kwargs)
191 except pymongo.errors.ConnectionFailure as exc:
192 self.connection_attempts.append(f"failed to connect client {client!r}: {exc}")
193 return None
194 finally:
195 client.close()
196
197 def _init_client(self):
198 self.mongoses = []

Calls 4

closeMethod · 0.95
updateMethod · 0.45
MongoClientMethod · 0.45
commandMethod · 0.45