繁体   English   中英

导入错误:在 VM 上通过 apache2 和 wsgi 提供flask 应用程序时,无法从“flask”导入名称“request”

[英]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')

附加信息:

  1. 我已经在我的工作目录中设置了一个flask-env,但不确定是否有必要。
  2. 我已经尝试启动命令 pip install flask, pip install request。
  3. PIP 版本是来自 /usr/lib/python2.7/dist-packages/pip (python 2.7) 的 pip 18.1
  4. Flask 版本是 Flask 1.1.1
  5. App.run() 已被我的应用程序评论,因为据我所知不再需要,因为 wsgi 已经在为该应用程序提供服务。

谢谢你的帮助!

如果您想接收服务器传递的信息,您可以简单地执行以下操作:

@app.route('/')
def hello():
    if request.method == 'GET':
         return 'It is HTTP get!'

您不必导入“请求”。 还有 requests 包,但我认为您不打算使用它(它是为了通过 python 发出请求):)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM