0

我在http://flask.pocoo.org/snippets/24/上尝试了使 Flask 在 CheryPy 服务器上工作的方法,它工作正常,到目前为止没有问题。但除了“/”之外,我无法路由其他任何东西。例如,如果我想路由到 localhost/xyz,它会返回“Hello World”。

我应该为每条路径创建一个 Flask 应用程序吗?

server.py 是:

from cherrypy import wsgiserver
from qwe import app

d=wsgiserver.WSGIPathInfoDispatcher({"/":app,"/xyz":app})
server=wsgiserver.CherryPyWSGIServer(('0.0.0.0',80),d)

if __name__ == '__main__':
    try:
        server.start()
    except KeyboardInterrupt:
        server.stop()

qwe.py 是:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World"

@app.route("/xyz")
def ff():
    return "Test1"
4

1 回答 1

2

它应该以这种方式工作:

应用程序.py:

from flask import Flask
app = Flask(__name__)

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

@app.route("/xyz")
def xyz():
    return "Hello XYZ!"

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

服务器.py:

from cherrypy import wsgiserver
from app import app

d = wsgiserver.WSGIPathInfoDispatcher({'/': app})
server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 9999), d)

if __name__ == '__main__':
    try:
        server.start()
    except KeyboardInterrupt:
        server.stop()

去拿,http://localhost:9999然后去拿Hello World!http://localhost:9999/xyzHello XYZ!

于 2012-08-28T12:12:59.707 回答