Python
Scenarios
DDS allows you to perform data operations using Python. You can connect to a DB instance through an SSL connection or a non-SSL connection. SSL connections provide stronger security.
By default, SSL is disabled for new DDS instances. For details about how to enable SSL, see Configuring SSL.
This section describes how to connect to a single node instance using Python.
Prerequisites
- Ensure that the ECS can communicate with the DDS instance. To test the connectivity to the instance's IP address and port, run the curl command:
curl ip:port
If the message "It looks like you are trying to access MongoDB over HTTP on the native driver port." is displayed, the ECS and DDS instance can communicate with each other.
- Install Python and third-party installation package pymongo on the ECS. Pymongo 2.8 is recommended.
- If SSL is enabled, you need to download the root certificate and upload it to the ECS.
Enabling and Disabling SSL
import ssl
from pymongo import MongoClient
conn_urls="mongodb://rwuser:rwuserpassword@ip:port/{mydb}?authSource=admin&directConnection=true"
connection = MongoClient(conn_urls,connectTimeoutMS=5000,ssl=True, ssl_cert_reqs=ssl.CERT_REQUIRED,ssl_match_hostname=False,ssl_ca_certs=${path to certificate authority file})
dbs = connection.database_names()
print "connect database success! database names is %s" % dbs import ssl
from pymongo import MongoClient
conn_urls="mongodb://rwuser:rwuserpassword@ip:port/{mydb}?authSource=admin&directConnection=true"
connection = MongoClient(conn_urls,connectTimeoutMS=5000)
dbs = connection.database_names()
print "connect database success! database names is %s" % dbs
- The authentication database in the URL must be admin. That means setting authSource to admin.
- In SSL mode, you need to manually generate the trustStore file.
- The authentication database must be admin, and then switch to the service database.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot