我正在尝试创建一个。由于Flask开发服务器不适合生产,所以我尝试使用cherrypy应用服务器。
下面是我试图通过cherrypy公开的烧瓶应用程序
from flask import Flask,request
from flask_restful import Api,Resource, reqparse
app= Flask(__name__)
api = Api(app)
class Main (Resource):
def get(self):
return "Hello Flask"
if __name__ == '__main__':
api.add_resource(Main, "/testapp/")
app.run(debug=True)下面是我创建的cherrypy脚本
try:
from cheroot.wsgi import Server as WSGIServer, PathInfoDispatcher
except ImportError:
from cherrypy.wsgiserver import CherryPyWSGIServer as WSGIServer, WSGIPathInfoDispatcher as PathInfoDispatcher
from stack import app
d = PathInfoDispatcher({'/': app})
server = WSGIServer(('127.0.0.1', 8080), d)
if __name__ == '__main__':
try:
server.start()
print("started")
except KeyboardInterrupt:
server.stop()我已经将这个脚本保存为项目目录中的"run.py“。当我运行这个程序时,它不会显示任何错误,这使得我对它进行细化,这是正确的。
但不幸的是,我不能使用url来访问这个
从理论上讲,这个API的url应该是类似于下面的http://127.0.0.1:8080/testapp/
但是它会抛出404和消息。
“服务器上找不到所请求的URL。如果手动输入URL,请检查拼写,然后再试一次。”
我做错什么了?
发布于 2018-10-20 22:00:14
这个
api.add_resource(Main, "/testapp/")在您的文件中,如果stack.py作为条件包含在run.py中,则不会执行该文件。
if __name__ == '__main__':
...不为真(在stack.py上下文中)。
将调用移动到api.add_resource(.)对于一个位于if-main-条件之外的位置(因此它总是被执行),应该解决这个问题。
https://stackoverflow.com/questions/52405131
复制相似问题