“TypeError”:'list'对象不是可调用的烧瓶

时间:2014-12-22 19:12:46

标签: python flask

我正在尝试使用flask显示浏览器中已连接设备的列表。我在端口8000上启用了烧瓶:

在server.py中:

@server.route('/devices',methods = ['GET'])
def status(): 
    return app.stat()

if __name__ == '__main__':
        app.run()
在app.py中

def stat():
    return(glob.glob("/dev/tty57") + glob.glob("/dev/tty9"))

这是我的考验:

url = "http://127.0.0.1:8000"

response = requests.get(url + "").text
print response

但我一直收到这个错误:

"TypeError": 'list' object is not callable.

我在检查ttyUSB,...和其他设备是否存在时做错了吗?

1 个答案:

答案 0 :(得分:30)

问题是您的端点正在返回一个列表。 Flask只喜欢某些返回类型。可能最常见的两个是

  • 一个Response对象
  • a str(以及Python 2.x中的unicode

您还可以返回任何可调用对象,例如函数。

如果您想要返回设备列表,您有几个选择。您可以将列表作为字符串返回

@server.route('/devices')
def status():
    return ','.join(app.statusOfDevices())

或者如果您希望能够将每个设备视为单独的值,则可以返回JSON响应

from flask.json import jsonify

@server.route('/devices')
def status():
    return jsonify({'devices': app.statusOfDevices()})
    # an alternative with a complete Response object
    # return flask.Response(jsonify({'devices': app.statusOfDevices()}), mimetype='application/json')