鍍金池/ 問(wèn)答/Python/ flask 逐行顯示shell返回內(nèi)容

flask 逐行顯示shell返回內(nèi)容

有個(gè)很簡(jiǎn)單的需求,想在頁(yè)面讀一些linux狀態(tài),現(xiàn)在能弄成list 但有沒(méi)有辦法逐行限制。

@app.route("/status")
def status():
    p = os.popen('netstat -ntpl')
    x=p.readlines()
    print x
    return str(x)

或者說(shuō)我怎么把格式弄成json 前端用js去排版?

clipboard.png

回答
編輯回答
愛(ài)是癌

return '<br>'.join(x)

2017年4月8日 10:21
編輯回答
無(wú)標(biāo)題

x應(yīng)該就是字符串列表了,可以直接轉(zhuǎn)成json的

from json import dumps
x = ['aaa','bbb','hello world']
print(dumps(x)) # ["aaa", "bbb", "hello world"]
2017年10月5日 00:27
編輯回答
忠妾

建議使用flask-socketio。websocket還是很有用處的。

2017年6月17日 11:32