You should be able to escape the password using urllib.quote(). Although you should only quote/escape the password, and exclude the username: ;
otherwise the : will also be escaped into %3A.
For example:
import pymongo
import urllib
mongo_uri = "mongodb://username:" + urllib.parse.quote("p@ssword") + "@127.0.0.1:27001/"
client = pymongo.MongoClient(mongo_uri)
The above snippet was tested for MongoDB v3.2.x and PyMongo v3.2.2.
The example above assumed in the MongoDB URI connection string:
- The user is created in the
admindatabase. - The host
mongodrunning on is 127.0.0.1 (localhost) - The port
mongodassigned to is 27001