【问题标题】:Bottle App gives Internal Server Error when accessing default page访问默认页面时,Bottle App 出现内部服务器错误
【发布时间】:2021-01-08 08:34:40
【问题描述】:

我刚刚开始使用 Bottle。我在GitHub 上有一个示例应用程序。主模块 app.py(在 Application 文件夹中)如下所示

"""
This script runs the application using a development server.
"""

import bottle
import os
import sys

# routes contains the HTTP handlers for our server and must be imported.
import routes

if '--debug' in sys.argv[1:] or 'SERVER_DEBUG' in os.environ:
    # Debug mode will enable more verbose output in the console window.
    # It must be set at the beginning of the script.
    bottle.debug(True)

def wsgi_app():
    """Returns the application to make available through wfastcgi. This is used
    when the site is published to Microsoft Azure."""
    return bottle.default_app()

if __name__ == '__main__':
    PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
    STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static').replace('\\', '/')
    HOST = os.environ.get('SERVER_HOST', 'localhost')
    try:
        PORT = int(os.environ.get('SERVER_PORT', '5555'))
    except ValueError:
        PORT = 5555

    @bottle.route('/static/<filepath:path>')
    def server_static(filepath):
        """Handler for static files, used with the development server.
        When running under a production server such as IIS or Apache,
        the server should be configured to serve the static files."""
        return bottle.static_file(filepath, root=STATIC_ROOT)

    # Starts a local test server.
    bottle.run(server='wsgiref', host=HOST, port=PORT)

requirements.txt 文件有

bottle
gunicorn

作为依赖项。我正在使用 Python 3.7.2。运行 pip install -r requirements.txt 后,

我运行了 python app.py。服务器启动,我可以访问默认页面没有错误错误

我尝试使用 gunicorn 运行服务器,如下所示

gunicorn -w 2 -b 0.0.0.0:8080 app:wsgi_app    

服务器启动正常,但是当我访问默认页面时,我得到了

Traceback (most recent call last):
  File "/Users/<user>/Codebase/test-bottle/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 134, in handle
    self.handle_request(listener, req, client, addr)
  File "/Users/<user>/Codebase/test-bottle/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 175, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
TypeError: wsgi_app() takes 0 positional arguments but 2 were given

请让我知道我做错了什么。

【问题讨论】:

    标签: python python-3.x gunicorn bottle


    【解决方案1】:

    您可以使用“应用程序工厂”模式(如 gunicorn documentation 中所述)在不修改代码的情况下运行应用程序:

    gunicorn -w 2 -b 0.0.0.0:8080 'app:wsgi_app()'
    

    请注意,您的页面不会完全显示,想。静态链接,例如css,不会加载,因为在这种情况下您没有在代码中定义任何路由:

    def wsgi_app():
        """Returns the application to make available through wfastcgi. This is used
        when the site is published to Microsoft Azure."""
        return bottle.default_app()
    

    【讨论】:

      【解决方案2】:

      您尝试过以下方法吗?

      from bottle import route, run, static_file
      
      @route('/')
      def index():
          return '<h1>Hello Bottle!</h1>'
          @bottle.route('/static/<filepath:path>')
      
      @route("/static//<filepath:re:.*")
      def server_static(filepath):
          """Handler for static files, used with the development server.
          When running under a production server such as IIS or Apache,
          the server should be configured to serve the static files."""
          return bottle.static_file(filepath, root=STATIC_ROOT)
      
      
      
      if __name__ == "__main__":
          run(host='localhost', port=5555)
      
      app = bottle.default_app()
      

      然后在控制台中:

      gunicorn -w 4 your_app_name:app
      

      【讨论】:

      • 我不想修改应用代码。在不更改应用程序代码的情况下,我如何使用 gunicorn 运行它?当前代码有什么限制吗?
      猜你喜欢
      • 1970-01-01
      • 2020-04-27
      • 2011-03-16
      • 1970-01-01
      • 2013-11-28
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多