【问题标题】:AttributeError: module 'wsgi' has no attribute 'application'AttributeError: 模块 \'wsgi\' 没有属性 \'application\'
【发布时间】:2022-08-10 14:55:43
【问题描述】:

app.py 文件代码:

import webbrowser
import time

#!/usr/bin/env python

try:
    # For Python 3.0 and later
    from urllib.request import urlopen
except ImportError:
    # Fall back to Python 2\'s urllib2
    from urllib2 import urlopen

import certifi
import json

def get_jsonparsed_data(url):
    \"\"\"
    Receive the content of ``url``, parse it as JSON and return the object.

    Parameters
    ----------
    url : str

    Returns
    -------
    dict
    \"\"\"
    response = urlopen(url, cafile=certifi.where())
    data = response.read().decode(\"utf-8\")
    return json.loads(data)

url = (\"https://financialmodelingprep.com/api/v3/quote/AAPL,FB?apikey=d099f1f81bf9a62d0f16b90c3dc3f718\")
print(get_jsonparsed_data(url))

country = get_jsonparsed_data(url)
count = 0
for result in country:
    if count == 0:
        header = result.keys()
        for head in header:
            html_content = f\"<div> {head} </div>\"
        count += 1


with open(\"index.html\", \"w\") as html_file:
    html_file.write(html_content)
    print(\"Html file created successfully !!\")

    time.sleep(2)
    webbrowser.open_new_tab(\"index.html\")

passenger_wsgi.py 文件代码:

import imp
import os
import sys


sys.path.insert(0, os.path.dirname(__file__))

wsgi = imp.load_source(\'wsgi\', \'app.py\')
application = wsgi.application

错误:

Traceback (most recent call last):
      File \"/home/stockpee/staging/passenger_wsgi.py\", line 9, in <module>
        application = wsgi.application
    AttributeError: module \'wsgi\' has no attribute \'application\'
    Traceback (most recent call last):
      File \"/home/stockpee/staging/passenger_wsgi.py\", line 9, in <module>
        application = wsgi.application
    AttributeError: module \'wsgi\' has no attribute \'application\'

你好, 大家,我是 Python 新手。我在本地机器上开发了一个基本应用程序。但是当我将它部署在 A2Host 托管服务器上时。当我在网络浏览器中运行我的应用程序时,我遇到了上述错误。

有没有人帮我解决上述问题。我会非常感谢那个人。

  • 托管服务器(和代码 passenger_wsgi.py)需要运行 web server 的脚本 - 即。使用模块Flask 和行application = Flask(__file__) - 但您在app.py 中的代码不是Web 服务器。您只有普通脚本,不能将其作为 Web 服务器运行。最终,您可以使用cron 定期运行它。
  • 函数webbrowser 将尝试在服务器上打开 Web 浏览器并将其显示在直接连接到服务器的显示器上 - 而不是在本地显示器上。但是服务器没有显示器,他们甚至不使用 windows 运行图形模式,而只运行文本模式。所以它甚至不能启动网络浏览器。
  • 您必须学习如何使用 FlaskDjangoBottle 等模块运行 Web 服务器。而且您必须了解 Web 服务器的工作原理——它们无法直接访问本地设备。 Web 浏览器将 URL 发送到服务器,并返回浏览器显示的 HTML。所有这些工作都与普通脚本不同。 Web 浏览器不能直接访问 Python 代码中的变量——有时它可能需要 JavaScript。

标签: python python-3.x


【解决方案1】:

我会提前为我的简短回答道歉。但这与您尚未建立威士忌应用程序有关。

您需要确保在您的主应用程序文件 app.py 中建立您的应用程序。

def app(environ, start_response):
    pprint("whoo hooo.  ")

我希望这有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 2019-02-18
    • 1970-01-01
    • 2020-01-01
    • 2019-07-20
    • 2021-11-05
    相关资源
    最近更新 更多