ImportError: cannot import name 'request' from 'flask' when serving flask app via apache2 and wsgi on VM
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$
我的mailService.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')
附加信息:
- 我已经在我的工作目录中设置了一个 flask-env,但不确定是否有必要。
- 我已经尝试启动命令 pip install flask,pip install request。
- PIP 版本是来自 /usr/lib/python2.7/dist-packages/pip (python 2.7)
的 pip 18.1
- Flask 版本为 Flask 1.1.1
- App.run() 已被我的应用程序评论,因为据我了解不再需要,因为 wsgi 已经在为该应用程序提供服务。
感谢您的帮助!
如果你想接收你的服务器传递过来的,你可以简单地做这样的事情:
@app.route('/')
def hello():
if request.method == 'GET':
return 'It is HTTP get!'
您不必导入 'request'。还有 requests 包,但我认为你不是打算使用它(它是由 python 提出请求):)
所以我将我的 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$
我的mailService.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')
附加信息:
- 我已经在我的工作目录中设置了一个 flask-env,但不确定是否有必要。
- 我已经尝试启动命令 pip install flask,pip install request。
- PIP 版本是来自 /usr/lib/python2.7/dist-packages/pip (python 2.7) 的 pip 18.1
- Flask 版本为 Flask 1.1.1
- App.run() 已被我的应用程序评论,因为据我了解不再需要,因为 wsgi 已经在为该应用程序提供服务。
感谢您的帮助!
如果你想接收你的服务器传递过来的,你可以简单地做这样的事情:
@app.route('/')
def hello():
if request.method == 'GET':
return 'It is HTTP get!'
您不必导入 'request'。还有 requests 包,但我认为你不是打算使用它(它是由 python 提出请求):)