您的位置:首页 > 编程语言 > Python开发

Python作为移动客户端后台服务器

2016-02-26 17:32 731 查看
python+flask实现服务器:

安装python和flask略过,servicestest.py代码:

from flask import Flask, jsonify

app = Flask(__name__)

tasks = [
{
'id':1,
'title':u'Buy groceries',
'description':u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id':2,
'title':u'Learn Python',
'description':u'Need to find a good Python tutorial on the web',
'done': False
}
]

@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
return jsonify({'tasks': tasks})

if __name__ == '__main__':
app.run(debug=True)
命令行下:python servicestest.py

之后浏览器:http://127.0.0.1:5000/todo/api/v1.0/tasks就可以看到运行的结果

运行结果:

{
tasks: [
{
description: "Milk, Cheese, Pizza, Fruit, Tylenol",
done: false,
id: 1,
title: "Buy groceries"
},
{
description: "Need to find a good Python tutorial on the web",
done: false,
id: 2,
title: "Learn Python"
}
]
}
建议使用curl:









curl -i http://localhost:5000/todo/api/v1.0/tasks[/code] 运行结果:



虽然很简单,毕竟是第一次能运行,还是记录鼓励下自己。

参考:http://www.cnblogs.com/vovlie/p/4178077.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: