“TypeError":“list"对象不是可调用的烧瓶 [英] "TypeError": 'list' object is not callable flask

查看:24
本文介绍了“TypeError":“list"对象不是可调用的烧瓶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to show the list of connected devices in browser using flask. I enabled flask on port 8000:

在 server.py 中:

in server.py:

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

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

在 app.py 中:

in 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、... 和其他设备是否存在时做错了什么?

Am I doing sth wrong in checking if ttyUSB, ... and other devices existing?

推荐答案

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

The problem is that your endpoint is returning a list. Flask only likes certain return types. The two that are probably the most common are

  • 一个 Response 对象
  • a str(以及 Python 2.x 中的 unicode)
  • a Response object
  • a str (along with unicode in Python 2.x)

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

You can also return any callable, such as a function.

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

If you want to return a list of devices you have a couple of options. You can return the list as a string

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

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

or you if you want to be able to treat each device as a separate value, you can return a JSON response

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')

这篇关于“TypeError":“list"对象不是可调用的烧瓶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆