Python Flask:未发生API调用,导致500个内部服务器错误

时间:2019-06-25 02:14:16

标签: python flask flask-restful

我对Python Flask还是很陌生,请使用flask_restplus设置一个简单的项目来进行基本的API调用。我可以在庞大的页面中看到我的API,但这导致500个内部服务器错误

run.py:

import os

from flask_script import Manager
from app.main import create_app

from app.app import blueprint

app = create_app(os.getenv('BOILERPLATE_ENV') or 'dev')
app.register_blueprint(blueprint)

app.app_context().push()

manager = Manager(app)


@manager.command
def run():
    app.run(threaded=True)


if __name__ == '__main__':
    manager.run()

app.py:

from flask_restplus import Api
from flask import Blueprint
from .main.controller.hello_controller import api as hello

blueprint = Blueprint('api', __name__)

api = Api(blueprint,
          title='My RESTful API',
          version='1.0',
          description='My flask restplus web service'
          )

api.add_namespace(hello, path='/hello')

hello_controller.py:

import json
from flask import request
from flask_restful import Resource

api = Namespace('hello', description='hello world')

@api.route('/', methods=['GET'])
class Hello(Resource):
    @api.param('name1', 'Name1')
    @api.param('name2', 'Name2')
    @api.response(200, 'hello said successfully')
    @api.doc('Hello Names')
    def get(self, name1, name2):
        """ Get Hello names """
        return json.dump("Hello" + name1 + "and Hello" + name2), 200

也尝试过这两种返回类型,但是没有运气。

return json.dumps({'data': "Hello" + name1 + "and Hello" + name2, 'success': 200})

return Response(json.dumps("Hello" + name1 + "and Hello" + name2), mimetype='application/json')

当我尝试从swagger UI执行上述API调用时,结果为500 Internal server error。首先,它甚至没有达到我的API路由/方法。这是来自控制台的错误:

    ERROR:flask.app:Exception on /hello/ [GET]
Traceback (most recent call last):
  File "/Users/myuser/PycharmProjects/myDev/venv/lib/python3.6/site-packages/flask/app.py", line 1832, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/myuser/PycharmProjects/myDev/venv/lib/python3.6/site-packages/flask/app.py", line 1818, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/myuser/PycharmProjects/myDev/venv/lib/python3.6/site-packages/flask_restplus/api.py", line 325, in wrapper
    resp = resource(*args, **kwargs)
  File "/Users/myuser/PycharmProjects/myDev/venv/lib/python3.6/site-packages/flask/views.py", line 87, in view
    self = view.view_class(*class_args, **class_kwargs)
TypeError: object() takes no parameters
INFO:werkwal:127.0.0.1 - - [24/Jun/2019 21:43:27] "GET /hello/ HTTP/1.1" 500 -

如果我尝试使用以下路线:@api.route('/<name1>/<name2>', methods=['GET']),并从头开始执行,直到永远加载,但什么也没有发生。仍然错误是相同的:

    ERROR:flask.app:Exception on /hello/ [GET]
Traceback (most recent call last):
  File "/Users/myuser/PycharmProjects/myDev/venv/lib/python3.6/site-packages/flask/app.py", line 1832, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/myuser/PycharmProjects/myDev/venv/lib/python3.6/site-packages/flask/app.py", line 1818, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/myuser/PycharmProjects/myDev/venv/lib/python3.6/site-packages/flask_restplus/api.py", line 325, in wrapper
    resp = resource(*args, **kwargs)
  File "/Users/myuser/PycharmProjects/myDev/venv/lib/python3.6/site-packages/flask/views.py", line 87, in view
    self = view.view_class(*class_args, **class_kwargs)
TypeError: object() takes no parameters
    INFO:werkwla:127.0.0.1 - - [24/Jun/2019 21:19:29] "GET /hello/?name1=mike&name2=sam HTTP/1.1" 500 -

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

更新:您在flask_restful中导入flask_restplus而不是hello_controller。这个示例有一些错误(我认为是从复制到SO)。因此,很难说出什么是真正的错误或复制错误。如果没有一个干净的例子,那是我能做到的最好的,对不起!有关使用蓝图https://flask-restplus.readthedocs.io/en/stable/scaling.html#use-with-blueprints

的示例,请参见此处