【问题标题】:FileNotFoundError on Python Flask app hosted on Ubuntu-Apache with WSGI-Mod使用 WSGI-Mod 托管在 Ubuntu-Apache 上的 Python Flask 应用程序上的 FileNotFoundError
【发布时间】:2021-05-20 04:16:56
【问题描述】:

我有一个使用 WSGI-mod 托管在 ubuntu-apache 上的 python 烧瓶应用程序。它在我的本地 Windows 机器上完美运行,但在 ubuntu 机器上我似乎无法通过我的 python 脚本访问'./static/images/'?其他一切正常,但这段代码由于某种原因不起作用并产生此错误。

我已尝试四处搜索和编辑 VirtualHost,但似乎没有任何效果。无法解决这个问题。有什么想法吗?

给出错误的代码:

if session.get('logged_in'):
        directory = r'./static/images/'
        urls = []
        for filename in os.listdir(directory):
            if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".jpeg"):
                urls.append(os.path.join(directory, filename))
            else:
                continue

错误信息:

[2021 年 2 月 17 日星期三 10:13:10.857968] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] [2021-02-17 10:13:10,856] 应用程序中的错误:/cdn 上的异常-controlpanel [GET],引用者:website-uri [2021 年 2 月 17 日星期三 10:13:10.858012] [wsgi:error] [pid 7746:tid 140653174441728] [客户端 IP] Traceback (最近一次通话最后):,referer: website-uri [2021 年 2 月 17 日星期三 10:13:10.858019] [wsgi:error] [pid 7746:tid 140653174441728] [客户端 IP] 文件“/usr/local/lib/python3.8/dist-packages/flask/app.py” ,第 2447 行,在 wsgi_app 中,引用:website-uri [2021 年 2 月 17 日星期三 10:13:10.858025] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] response = self.full_dispatch_request(), referer: website-uri [2021 年 2 月 17 日星期三 10:13:10.858030] [wsgi:error] [pid 7746:tid 140653174441728] [客户端 IP] 文件“/usr/local/lib/python3.8/dist-packages/flask/app.py” ,第 1952 行,在 full_dispatch_request 中,referer: website-uri> [2021 年 2 月 17 日星期三 10:13:10.858035] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] rv = self.handle_user_exception(e), referer: website-uri [2021 年 2 月 17 日星期三 10:13:10.858040] [wsgi:error] [pid 7746:tid 140653174441728] [客户端 IP] 文件“/usr/local/lib/python3.8/dist-packages/flask/app.py” ,第 1821 行,在 handle_user_exception 中,referer: website-uri> [2021 年 2 月 17 日星期三 10:13:10.858045] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] reraise(exc_type, exc_value, tb), referer: website-uri [2021 年 2 月 17 日星期三 10:13:10.858050] [wsgi:error] [pid 7746:tid 140653174441728] [客户端 IP] 文件“/usr/local/lib/python3.8/dist-packages/flask/_compat.py” ,第 39 行,在 reraise 中,引用:website-uri [2021 年 2 月 17 日星期三 10:13:10.858055] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] raise value, referer: website-uri [2021 年 2 月 17 日星期三 10:13:10.858060] [wsgi:error] [pid 7746:tid 140653174441728] [客户端 IP] 文件“/usr/local/lib/python3.8/dist-packages/flask/app.py” ,第 1950 行,在 full_dispatch_request 中,引用者:website-uri> [2021 年 2 月 17 日星期三 10:13:10.858066] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] rv = self.dispatch_request(), referer: website-uri [2021 年 2 月 17 日星期三 10:13:10.858071] [wsgi:error] [pid 7746:tid 140653174441728] [客户端 IP] 文件“/usr/local/lib/python3.8/dist-packages/flask/app.py” ,第 1936 行,在 dispatch_request 中,referer: website-uri> [2021 年 2 月 17 日星期三 10:13:10.858076] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] return self.view_functionsrule.endpoint, referer: website-uri [2021 年 2 月 17 日星期三 10:13:10.858081] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/var/www/FlaskApp/App/app.py", line 99, in cdnControlpanel, referer : 网站-uri [2021 年 2 月 17 日星期三 10:13:10.858086] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] for filename in os.listdir(directory):, referer: website-uri [2021 年 2 月 17 日星期三 10:13:10.858130] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] FileNotFoundError: [Errno 2] No such file or directory: './static/images/', referer:网站-uri

【问题讨论】:

    标签: python apache ubuntu flask wsgi


    【解决方案1】:

    想通了。在 python 中声明目录路径时,“。”不是相对于当前文件,而是相对于当前工作目录,可以通过 os.chdir('path/to/dir') 进行更改

    【讨论】:

      猜你喜欢
      • 2016-03-15
      • 2014-04-11
      • 2021-11-10
      • 2022-12-30
      • 2014-09-09
      • 2020-08-22
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多