Skip to content

Commit c11c6a2

Browse files
committed
PYTHON-1161 - Add Unix domain socket examples
1 parent 25fd33b commit c11c6a2

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

pymongo/mongo_client.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,40 @@ def __init__(
101101
database or auth information are passed, the last database,
102102
username, and password present will be used. For username and
103103
passwords reserved characters like ':', '/', '+' and '@' must be
104-
escaped following RFC 2396.
104+
percent encoded following RFC 2396::
105+
106+
try:
107+
# Python 3.x
108+
from urllib.parse import quote_plus
109+
except ImportError:
110+
# Python 2.x
111+
from urllib import quote_plus
112+
113+
uri = "mongodb://%s:%s@%s" % (
114+
quote_plus(user), quote_plus(password), host)
115+
client = MongoClient(uri)
116+
117+
Unix domain sockets are also supported. The socket path must be percent
118+
encoded in the URI::
119+
120+
uri = "mongodb://%s:%s@%s" % (
121+
quote_plus(user), quote_plus(password), quote_plus(socket_path))
122+
client = MongoClient(uri)
123+
124+
But not when passed as a simple hostname::
125+
126+
client = MongoClient('/tmp/mongodb-27017.sock')
105127
106128
.. warning:: When using PyMongo in a multiprocessing context, please
107129
read :ref:`multiprocessing` first.
108130
109131
:Parameters:
110-
- `host` (optional): hostname or IP address of a single mongod or
111-
mongos instance to connect to, or a mongodb URI, or a list of
112-
hostnames / mongodb URIs. If `host` is an IPv6 literal
113-
it must be enclosed in '[' and ']' characters following
114-
the RFC2732 URL syntax (e.g. '[::1]' for localhost). Multihomed
115-
and round robin DNS addresses are **not** supported.
132+
- `host` (optional): hostname or IP address or Unix domain socket
133+
path of a single mongod or mongos instance to connect to, or a
134+
mongodb URI, or a list of hostnames / mongodb URIs. If `host` is
135+
an IPv6 literal it must be enclosed in '[' and ']' characters
136+
following the RFC2732 URL syntax (e.g. '[::1]' for localhost).
137+
Multihomed and round robin DNS addresses are **not** supported.
116138
- `port` (optional): port number on which to connect
117139
- `document_class` (optional): default class to use for
118140
documents returned from queries on this client

0 commit comments

Comments
 (0)