Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
777 views
in Technique[技术] by (71.8m points)

python - "TypeError": 'list' object is not callable flask

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

in server.py:

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

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

in app.py:

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

And this is my test:

url = "http://127.0.0.1:8000"

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

but I keep getting this error:

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

  • 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())

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...