Python 将mongodb与烧瓶连接

Python 将mongodb与烧瓶连接,python,mongodb,flask,Python,Mongodb,Flask,我想将我的flask应用程序与Mongodb cloud连接,但当我运行它时,它在验证函数中的行(44,如果条件)显示一个错误,Mongodb代码开始执行。 这是我的代码,它返回 pymongo.errors.ServerSelectionTimeoutError:连接已关闭、连接已关闭、连接已关闭 我如何解决这个问题,我认为这基本上是mongodb云数据库与flak的连接问题 当我将它与本地主机mongodb连接时,它将成功执行 但当我将其与mongodb云连接时,它会生成ServerSele

我想将我的flask应用程序与Mongodb cloud连接,但当我运行它时,它在验证函数中的行(44,如果条件)显示一个错误,Mongodb代码开始执行。
这是我的代码,它返回

pymongo.errors.ServerSelectionTimeoutError:连接已关闭、连接已关闭、连接已关闭

我如何解决这个问题,我认为这基本上是mongodb云数据库与flak的连接问题
当我将它与本地主机mongodb连接时,它将成功执行


但当我将其与mongodb云连接时,它会生成
ServerSelectionTimeoutError

我建议您部署自己的mongodb进行测试,因为您的远程数据库服务器端似乎存在一些连接问题

在本地计算机上安装mongodb

您能给出终端的完整错误堆栈吗?我从您的mongodb连接字符串中发现了一些问题,第一个问题是您没有在主机名的末尾添加端口。ashish-hbjy0。mongodb.net根本无法访问。您在Atlas中设置了白名单了吗。默认情况下,远程访问是禁用的。是的,我成功地连接了,谢谢#肚皮炸弹这看起来并不是问题的答案question@Zun你有更好的主意吗?
from flask import Flask, render_template, request, url_for
import smtplib # for send message to mail
from random import randint # to generate random number
import re # for email validation
import pymongo # for connect with database
from pymongo import MongoClient
from flask_pymongo import PyMongo
from flask_mongoengine import MongoEngine

myclient = MongoClient("mongodb+srv://xxxxxx:Xxxxxx12@ashish-hbjy0.mongodb.net/test?retryWrites=true&w=majority")
mydb = myclient.emailverify  # database name
mycol = mydb["otp"]  # table name


app = Flask(__name__)
randotp = randint(1000, 9999)   # Random 4 digit OTP Generator
otp = randotp

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/verify', methods=['POST', 'GET'])
def verify():
    global email, otp
    if request.method == 'POST':
        info = request.form
        email = request.form.get('email')
        if info.get('email', None) is not None:
            if email in [temp['email'] for temp in mycol.find({}, {"_id":0, "email":1})]:  # at here it stuck
                global msg  # alreay verified message
                msg = "Email Already Verified"
                return render_template('status.html', result = email, message=msg)
            else:
                s = smtplib.SMTP('smtp.gmail.com', 587) 
                s.starttls() 
                s.login("vacancykey123@gmail.com", "@vacancyKey1")                
                otpmessage = "Your OTP " + str(otp)
                s.sendmail("vacancykey123@gmail.com", email, otpmessage)
                print("sent mail")

                #mycol.insert_one({ "email": email})
                print("email entered!")
                s.quit()
                return render_template('verify.html', result = info)


@app.route('/status', methods=['POST','GET'])
def status():
    if request.method == 'POST':
        sentotp = int(request.form.get('otp'))
        semail = email
        if sentotp == otp:
            mycol.insert_one({ "email": email})  # at here it stuck
            msg = "Email Verified"
            return render_template('status.html', email= semail, message = msg)
        else:
            msg = "Wrong OTP, Please check again!"
            return render_template("status.html", message = msg)


if __name__ == '__main__':
   app.run()