zoukankan      html  css  js  c++  java
  • python 部署 Restful web

    使用python web做Restful 风格,很简单,采用Flask框架轻松实现一个RESTful的服务。

    Restful相关介绍请查看:https://www.ibm.com/developerworks/library/ws-restful/index.html

    1. 环境搭建

    首先需要准备环境

      need virtualenv  python的沙盒环境--virtualenv (不必须)

        这里使用 virtualenv  来创建一个Python环境,用来运行我们稍后的restful服务端。

      need flask (必须)

    1.1 安装

    1.1.1 pip 

      安装软件用

      下载地址:https://pypi.python.org/pypi/pip/

      下载 pip tar.gz

      解压 tar -xzvf 

      进入目录安装 python setup.py install 即可使用

    1.1.2 virtualenv  

      创建虚拟环境用

      pip install virtualenv 直接下载安装

      或者 进入 https://pypi.python.org/pypi/virtualenv 下载 tar包 和pip安装类似

    1.1.3 flask

      web框架 重要

      pip install flask 直接下载安装

      或者 https://pypi.python.org/pypi/Flask/0.12.2#downloads 下载tar包 解压安装

      手动装需要(install会自动安装,如果linux虚拟机未联网,根据提示缺什么,去安装什么):

        click >=2.0:https://pypi.python.org/simple/click/

        itsdangerous >=0.21:https://pypi.python.org/simple/itsdangerous/

        jinja2 >= 2.4:https://pypi.python.org/simple/jinja2/

        werkzeug>=0.7:https://pypi.python.org/simple/werkzeug/

    2. 环境测试

    2.1 创建虚拟环境

    选择一个合适的路径下 

    我选择的是  /var/flask 当然可以随意选择一个目录

    virtualenv web ,做一个名为web的python环境,也可以不要这个环境,直接第二步创建app.py

    目录如:

      

    2.2 新建app.py

    在bin中新建app.py

    app.route 的用法和Springmvc 的controller requestmapping很类似,详细请查看:http://www.guadong.net/article/2eHheDFm.html

    #环境测试
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def index():
        return "Hello, Python Flask!"
    
    if __name__ == '__main__':
        app.run(host="0.0.0.0",debug=True)

    2.3 执行访问

    执行 python app.py

      

    本地访问(可以新开一个ssh进行测试,也可以用IP地址(ip访问需要绑定0.0.0.0)  app.run(host="0.0.0.0",debug=True)  用浏览器测试)

    这里采用linux内部访问:curl -i http://localhost:5000

      

    访问得到了Hello, Python Flask!

    说明环境搭建成功success!

    3. 正式干活

    这里还是使用Person对象来做Restful的演示,参考:SpringMVC 构建Restful风格 及问题处理

    /persons  GET         得所有person

    /person/{id}  GET     得到id的person

    /person   POST        新增person

    /person/{id}  PUT      更新id的person

    /person/{id}  DELETE    删除id的person

    新建person.py

    编码:

    #coding=utf-8

    引入需要的模块:

    from flask import Flask,jsonify,abort,make_response,request

    模拟数据库数据:

    #模拟数据库 person 属性 id name age done url
    persons = [
        {
            'id': 1,
            'name': u'loveincode',
            'age': 20, 
            'done': False,
            'url':u'loveincode.cnblogs.com'
        },
        {
            'id': 2,
            'name': u'strive',
            'age': 18, 
            'done': False,
            'url':u'loveincode.cnblogs.com'
        }
    ]

    3.1 GET persons

    代码设计:

    @app.route('/restful/persons', methods=['GET'])
    def get_persons():
        return jsonify({'persons': persons})

    先执行:python person.py

    test:curl -i http://localhost:5000/restful/persons

      

    3.2 GET person

    @app.route('/restful/person/<int:id>', methods=['GET'])
    def get_person(id):
        person = filter(lambda t: t['id'] == id, persons)
        if len(person) == 0:
            abort(404)
        return jsonify({'person': person[0]})

    先执行:python person.py

    test:curl -i http://localhost:5000/restful/person/2

      

    错误404 后面一起讲。

    3.3 POST

    @app.route('/restful/person', methods=['POST'])
    def create_person():
        if not request.json or not 'name' in request.json:
            abort(400)
        person = {
            'id': persons[-1]['id'] + 1,
            'name': request.json['name'],
            #如果没有提交age参数默认为20
            'age': request.json.get('age', 20),
            #同理如果没提交url,url也是默认值
            'url': request.json.get('url', "默认URL loveincode.cnblogs.com"),
            #该参数初始化FALSE
            'done': False
        }
        persons.append(person)
        return jsonify({'person': person}), 201

    先执行:python person.py

    test:提供三个测试

    curl -i -H "Content-Type: application/json" -X POST -d '{"name":"new loveincode"}' http://localhost:5000/restful/person

    curl -i -H "Content-Type: application/json" -X POST -d '{"name":"new loveincode","age":23}' http://localhost:5000/restful/person

    curl -i -H "Content-Type: application/json" -X POST -d '{"name":"new loveincode","age":23,"url":"1234"}' http://localhost:5000/restful/person

    验证最后一个 ,先post 再看所有 添加成功

      

    3.4 PUT

    @app.route('/restful/person/<int:id>', methods=['PUT'])
    def update_person(id):
        person = filter(lambda t: t['id'] == id, persons)
        if len(person) == 0:
            abort(404)
        if not request.json:
            abort(400)
        if 'name' in request.json and type(request.json['name']) != unicode:
            abort(400)
        if 'age' in request.json and type(request.json['age']) is not int:
            abort(400)
        if 'url' in request.json and type(request.json['url']) != unicode:
            abort(400)
        if 'done' in request.json and type(request.json['done']) is not bool:
            abort(400)
        person[0]['name'] = request.json.get('name', person[0]['name'])
        person[0]['age'] = request.json.get('age', person[0]['age'])
        person[0]['url'] = request.json.get('url', person[0]['url'])
        person[0]['done'] = request.json.get('done', person[0]['done'])
        return jsonify({'person': person[0]})

    先执行:python person.py

    test:

    curl -i -H "Content-Type: application/json" -X PUT -d '{"done":true}' http://localhost:5000/restful/person/2

    curl -i -H "Content-Type: application/json" -X PUT -d '{"name":"update","age":30}' http://localhost:5000/restful/person/2

      

    3.5 DELETE

    @app.route('/restful/person/<int:id>', methods=['DELETE'])
    def delete_person(id):
        person = filter(lambda t: t['id'] == id, persons)
        if len(person) == 0:
            abort(404)
        persons.remove(person[0])
        return jsonify({'result': True})

    先执行:python person.py

    test:

    curl -i -X DELETE http://localhost:5000/restful/person/2

      

    3.6 Error 处理

    对400 和 404 进行错误json封装,一个友好的错误提示

    @app.errorhandler(404)
    def not_found(error):
        return make_response(jsonify({'error': 'Not found'}), 404)
    
    @app.errorhandler(400)
    def not_found(error):
        return make_response(jsonify({'error': 'Request Error'}), 400)

    github地址:https://github.com/loveincode/python-restful-web

    参考:http://www.cnblogs.com/vovlie/p/4178077.html

  • 相关阅读:
    Fatal error compiling: invalid target release: 11 -> [Help 1]
    java -jar 设置日志位置
    wordpress 添加 显示磁盘剩余空间百分比的插件
    记录我个人对Telegram的了解
    Ubuntu 18.04 切换使用Python3
    基于 CI 1.7.x 的 项目使用新版本CI的文件缓存类库
    使用sqlyog工具同步两个相同结构的数据库之间的数据
    了解GTIN小记
    华为手机设置桌面图标角标提醒的实现
    Quill编辑器IOS下无法获取焦点的解决方法
  • 原文地址:https://www.cnblogs.com/loveincode/p/python_restful_web.html
Copyright © 2011-2022 走看看