"RuntimeError: Working outside of application context " with Python Flask app ( Sending gmail using scheduler )
"RuntimeError: Working outside of application context " with Python Flask app ( Sending gmail using scheduler )
app = Flask(__name__)
app.config.update(
MAIL_SERVER = 'smtp.gmail.com',
MAIL_PORT = 465,
MAIL_USE_SSL = True,
MAIL_USE_TLS = False,
MAIL_USERNAME = '******',
MAIL_PASSWORD = '******'
)
mail = Mail(app)
#CS50.net
def lookup(symbol):
"""Look up quote for symbol."""
# Contact API
try:
response = requests.get(f"https://api.iextrading.com/1.0/stock/{urllib.parse.quote_plus(symbol)}/quote")
response.raise_for_status()
except requests.RequestException:
return None
# Parse response
try:
quote = response.json()
return {
"name": quote["companyName"],
"price": float(quote["latestPrice"]),
"symbol": quote["symbol"]
}
except (KeyError, TypeError, ValueError):
return None
@app.route('/')
def print_date_time():
Symbol = "PG"
Symbol = lookup(Symbol)
msg = mail.send_message(
'PG',
sender='*****',
recipients=['******'],
body = "PG DROP BELOW 91 buy now"
)
scheduler = BackgroundScheduler()
scheduler.add_job(func=print_date_time, trigger="interval", seconds=10)
scheduler.start()
# Shut down the scheduler when exiting the app
atexit.register(lambda: scheduler.shutdown())
我使用 Python 使用 Flask 创建一个应用程序,如果满足条件,它将每 10 秒向我发送一次 Gmail。当我 运行 我得到这个应用程序时:
"RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information. "
我的想法是错误是由于我试图通过应用程序上下文之外的 Gmail 发送消息引起的。有任何想法吗 ??谢谢
您的函数 print_date_time
正在通过 app 上下文 之外的新线程执行,Mail 对象需要它。
您应该将带有 app 对象的参数传递给您的函数(装饰器路由不是必需的)。此参数的值为 current_app._get_current_object()
.
我已经重新实现了你的功能print_date_time
:
def print_date_time(app):
with app.app_context():
Symbol = "PG"
Symbol = lookup(Symbol)
msg = mail.send_message(
'PG',
sender='*****',
recipients=['******'],
body = "PG DROP BELOW 91 buy now"
)
app = Flask(__name__)
app.config.update(
MAIL_SERVER = 'smtp.gmail.com',
MAIL_PORT = 465,
MAIL_USE_SSL = True,
MAIL_USE_TLS = False,
MAIL_USERNAME = '******',
MAIL_PASSWORD = '******'
)
mail = Mail(app)
#CS50.net
def lookup(symbol):
"""Look up quote for symbol."""
# Contact API
try:
response = requests.get(f"https://api.iextrading.com/1.0/stock/{urllib.parse.quote_plus(symbol)}/quote")
response.raise_for_status()
except requests.RequestException:
return None
# Parse response
try:
quote = response.json()
return {
"name": quote["companyName"],
"price": float(quote["latestPrice"]),
"symbol": quote["symbol"]
}
except (KeyError, TypeError, ValueError):
return None
@app.route('/')
def print_date_time():
Symbol = "PG"
Symbol = lookup(Symbol)
msg = mail.send_message(
'PG',
sender='*****',
recipients=['******'],
body = "PG DROP BELOW 91 buy now"
)
scheduler = BackgroundScheduler()
scheduler.add_job(func=print_date_time, trigger="interval", seconds=10)
scheduler.start()
# Shut down the scheduler when exiting the app
atexit.register(lambda: scheduler.shutdown())
我使用 Python 使用 Flask 创建一个应用程序,如果满足条件,它将每 10 秒向我发送一次 Gmail。当我 运行 我得到这个应用程序时:
"RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information. "
我的想法是错误是由于我试图通过应用程序上下文之外的 Gmail 发送消息引起的。有任何想法吗 ??谢谢
您的函数 print_date_time
正在通过 app 上下文 之外的新线程执行,Mail 对象需要它。
您应该将带有 app 对象的参数传递给您的函数(装饰器路由不是必需的)。此参数的值为 current_app._get_current_object()
.
我已经重新实现了你的功能print_date_time
:
def print_date_time(app):
with app.app_context():
Symbol = "PG"
Symbol = lookup(Symbol)
msg = mail.send_message(
'PG',
sender='*****',
recipients=['******'],
body = "PG DROP BELOW 91 buy now"
)