![](/img/trans.png)
[英](WSGI - Apache2/Flask) ImportError: cannot import name app
[英]ImportError: cannot import name 'request' from 'flask' when serving flask app via apache2 and wsgi on VM
所以我将我的flask应用程序部署到我的linux debian VM并通过wsgi和apache2(lynx serverIpAdress/mailService)提供服务。
我的问题:
当我使用命令python mailService.py
运行应用程序时,它运行良好,但是当我尝试通过 apache2 访问它时,出现以下错误:
[wsgi:error] [pid 8771:tid 139918835648256] [client 192.168.1.50:37344] File "/var/www/Bulma/MailService/mailService.wsgi", line 5, in <module>
[wsgi:error] [pid 8771:tid 139918835648256] [client 192.168.1.50:37344] from mailService import app as application
[[wsgi:error] [client 192.168.1.50:37344] File "/var/www/Bulma/MailService/mailService.py", line 2, in <module>
[Thu Jan 09 08:45:42.595785 2020] [wsgi:error] [pid 8771:tid 139918835648256] [client 192.168.1.50:37344] from flask import request, jsonify,json
[Thu Jan 09 08:45:42.595839 2020] [wsgi:error] [pid 8771:tid 139918835648256] [client 192.168.1.50:37344] ImportError: cannot import name 'request' from 'flask' (unknown location)
[Thu Jan 09 08:53:41.179747 2020] [wsgi:error] [pid 8771:tid 139918877611776] [client 192.168.1.50:37366] mod_wsgi (pid=8771): Failed to exec Python script file '/var/www/Bulma/Mai$
我的邮件服务.py:
import flask
from flask import request,json
import smtplib
app = flask.Flask(__name__)
app.config["DEBUG"] = True
@app.route('/api/v1/resources/mailFactory',methods=['POST'])
def mailFactory():
Json = request.get_json()
Application = Json['Application']
context = int(Application)
switcher = {
0: themisMailService
}
if Application:
func = switcher.get(context)
return func(Json)
def themisMailService(Json):
sender_email = "test@gmail.com"
receiver_email = "test@gmail.com"
message = """From: From Person <from@fromdomain.com>
To: To Person <to@todomain.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
# Login Credentials for sending the mail
server.login(sender_email, "gjwvttjphdrhivoo") #app password inserted
server.sendmail(sender_email, receiver_email, message)
server.quit()
print ("Successfully sent email")
#app.run(host= '0.0.0.0')
附加信息:
谢谢你的帮助!
如果您想接收服务器传递的信息,您可以简单地执行以下操作:
@app.route('/')
def hello():
if request.method == 'GET':
return 'It is HTTP get!'
您不必导入“请求”。 还有 requests 包,但我认为您不打算使用它(它是为了通过 python 发出请求):)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.