Python 如何在git bash中使用try catch连接mongodb并捕获mondodb连接错误?
** a) 我想连接到MongoDB b) 用试扣挡块围住蒙多布 c) 捕获异常 我必须使用git来获取最新的分支Python 如何在git bash中使用try catch连接mongodb并捕获mondodb连接错误?,python,mongodb,bash,git,Python,Mongodb,Bash,Git,** a) 我想连接到MongoDB b) 用试扣挡块围住蒙多布 c) 捕获异常 我必须使用git来获取最新的分支 I have assumed that the below command will run before MongoDB connection: source setenv.sh source /opt/home/setenv.sh python3 mongodb.py python脚本应该有一个与/opt/home下的setenv.sh脚本源代码同名的shell包装器/ sh
I have assumed that the below command will run before MongoDB connection:
source setenv.sh
source /opt/home/setenv.sh
python3 mongodb.py
python脚本应该有一个与/opt/home下的setenv.sh脚本源代码同名的shell包装器/
shell包装器名称:mongodb.sh
python脚本名称:mongodb.py
我必须使用shell包装器来运行脚本
编码环境是jupyter实验室,我不知道如何使用它,这就是为什么我在这里问这个问题。我喜欢得到帮助
我使用以下代码连接mongo:
from urllib.parse
import quote_plus
import os
import json from pymongo
import MongoClient
import SSL
def main():
environment = "Git" if os.environ.get('ENVIRONMENT_NAME') is None else os.environ.get('ENVIRONMENT_NAME')
config_directory = os.path.abspath(os.path.join(os.path.dirname(__file__)))
with open(os.path.join(config_directory, "report_config.json")) as f:
common_configuration = json.load(f)
#mongo_db = common_configuration['mongo_db_'+environment]
mongo_url, mongo_db_name= create_mongo_url_db()
#print("mongo_url:%s, mongo_db_name:%s" %(mongo_url,mongo_db_name))
# mongo_url = "mongodb://%s:%s@%s:%s" % (
# quote_plus(mongo_db['user_name']), quote_plus(mongo_db['password']),
# quote_plus(mongo_db['host']), quote_plus(mongo_db['port']))
#mongo_db_name= mongo_db['mongo_db_name']
collection_name= "workflow"
db = MongoClient(mongo_url, ssl=True, ssl_cert_reqs=ssl.CERT_NONE)