【问题标题】:ImportError: cannot import name 'request' from 'flask' when serving flask app via apache2 and wsgi on VMImportError:通过虚拟机上的 apache2 和 wsgi 提供烧瓶应用程序时,无法从“flask”导入名称“request”
【发布时间】:2020-01-09 10:05:23
【问题描述】:

所以我将我的烧瓶应用程序部署到我的 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')

附加信息:

  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 已经在为该应用程序提供服务,因此不再需要它。

感谢您的帮助!

【问题讨论】:

    标签: python apache flask wsgi


    【解决方案1】:

    如果你想接收你的服务器传递的,你可以简单地做这样的事情:

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

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

    【讨论】:

    • 你好 Dawid,实际上我的服务已经到位,我只是觉得没有必要在我的帖子中添加它,因为它绝对与问题无关。现在是进口问题。也许不需要导入请求或 Json 之类的依赖项,我不知道我对烧瓶完全陌生,但我们肯定需要导入烧瓶来定义应用程序。
    • 我说的是,你不需要导入“请求”,它是在烧瓶应用程序中构建的——抱歉我不准确。当然需要导入flask。
    • 是的,你说得对,我不需要指定所有这些进口,只需要烧瓶一个:D。
    • 答案可能相关,但与帖子标题无关
    • 所以您投了反对票,因为标题与您一直在寻找的内容不符?也许改为调整标题?
    猜你喜欢
    • 2022-01-12
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 2018-12-31
    • 2012-07-28
    • 1970-01-01
    相关资源
    最近更新 更多