我正在使用 Flask 构建一个 Web 服务,并尝试使用 Paster 部署一个简单的“Hello, World”应用程序。不过,我很难将所有内容配置为协同工作。我已经看到 Google 热衷于使用 virtualenv 和 zcbuildout 通过粘贴来运行 Flask,但这对于一个非常基本的应用程序来说似乎有点过分了。现在,当我尝试使用我的应用程序加载 URL 时,出现以下错误:

Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/paste/httpserver.py", line 1068, in process_request_in_thread
  self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
  self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 639, in __init__
  self.handle()
File "/usr/lib/python2.7/dist-packages/paste/httpserver.py", line 442, in handle
  BaseHTTPRequestHandler.handle(self)
File "/usr/lib/python2.7/BaseHTTPServer.py", line 343, in handle
  self.handle_one_request()
File "/usr/lib/python2.7/dist-packages/paste/httpserver.py", line 437, in handle_one_request
  self.wsgi_execute()
File "/usr/lib/python2.7/dist-packages/paste/httpserver.py", line 287, in wsgi_execute
  self.wsgi_start_response)
AttributeError: GraffitiApp instance has no __call__ method

我的应用程序配置如下所示:

[DEFAULT]
loglevel = WARN
browser_cache_ttl = 30

[app:main]
use = egg:GraffitiService

[server:main]
use = egg:Paste#http
host = 0.0.0.0
port = %(graffiti_port)s

我的应用程序代码如下所示:

from flask import Flask

app = Flask(__name__)
app.debug = True

class GraffitiApp:

    @app.route('/')
    def hello_world():
        return "Hello World!"

    @app.route('/other')
    def other_page():
        return "Other page!"

    def main():
        app.run(debug = True)

if __name__ == "__main__":
    app.run(debug = True)

我需要创建一个 __call__() 方法并映射传递给适当函数的任何 URL,或者我需要对配置进行更改吗?

编辑

我正在使用名为 main.py 的工厂,因为我遇到了找不到名为“main”的入口点的错误:

import logging

from graffiti import GraffitiApp

def make_app(globalArgs, **localArgs):
    loglevelname = globalArgs.get("loglevel", "INFO").lower()

    if loglevelname == 'critical':
        loglevel = logging.CRITICAL
    elif loglevelname == 'debug':
        loglevel = logging.DEBUG
    elif loglevelname == 'error':
        loglevel = logging.ERROR
    elif loglevelname == 'fatal':
        loglevel = logging.FATAL
    elif loglevelname == 'info':
        loglevel = logging.INFO
    elif loglevelname == 'warn' or loglevelname == 'warning':
        loglevel = logging.WARN
    else:
        loglevel = logging.NOTSET
        loglevelname = 'notset'

    FORMAT = "%(asctime)s %(name)s %(levelname)s %(message)s"
    logging.basicConfig(format = FORMAT, level = loglevel)
    logging.info("Log level set to %s" % (loglevelname.upper()))

    return GraffitiApp()
有帮助吗?

解决方案

看起来服务器正在尝试运行您的 GraffitiApp() 类何时应该运行 Flask() 实例 app.

其他提示

offtop(对不起,但您的代码突破了我的眼睛):

import logging

from graffiti import GraffitiApp

def make_app(globalArgs, **localArgs):
    loglevelname = globalArgs.get("loglevel", "INFO")
    loglevel = getattr(logging, loglevelname.upper(), logging.NOTSET)


    FORMAT = "%(asctime)s %(name)s %(levelname)s %(message)s"
    logging.basicConfig(format = FORMAT, level = loglevel)
    logging.info("Log level set to %s" % (loglevelname))

    return GraffitiApp()
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top