zoukankan      html  css  js  c++  java
  • 教你分分钟搞定Python之Flask框架

    用最短的时间开发一个数据操作接口,Python是王道!

    一、安装pip

    1、首先检查linux有没有安装python-pip包,终端执行 pip -V
    [root@ network-scripts]# pip -V
    -bash: pip: command not found
    2、没有python-pip包就执行命令 yum -y install epel-release
    [root@ network-scripts]# yum -y install epel-release
    3、执行成功之后,再次执行yum -y install python-pip
    [root@ network-scripts]# yum install python-pip
    4、对安装好的pip进行升级 pip install --upgrade pip
    [root@ network-scripts]# pip install --upgrade pip
    至此,pip安装好了,执行pip -V 再次检查pip环境。
    [root@ network-scripts]# pip -V
    pip 9.0.1 from /usr/lib/python2.7/site-packages (python 2.7)

    二、下载Flask模块

    pip install flask

    三、新建app.py文件

    #!flask/bin/python
    # -*- coding: utf-8 -*-
    from flask import Flask,request
    import sys
    import os
    reload(sys)
    sys.setdefaultencoding('utf8')
    sys.path.append('./service')
    from a import AClass 
    app = Flask(__name__)
    @app.route('/', methods=['GET'])
    def index():
        return "Hello, World!"
    @app.route('/list', methods=['GET'])
    def id():
        m = request.args.get('m')
        return "你好!" + m
    @app.route('/cmd', methods=['GET'])
    def test():
        cmd = request.args.get('cmd')
        var=os.popen(cmd).read( )
        return  var
    @app.route('/pwd', methods=['GET'])
    def pwd():
        f = open('/etc/passwd','r')
        pwd = f.read()
        f.close()
        return pwd
    @app.route('/shell', methods=['GET'])
    def shell():
        var=os.popen('{ echo 1; }| ./service/git.sh').read( )
        return  var
    @app.route('/tt', methods=['GET'])
    def tt():
        a = AClass()
        return a.add()
    if __name__ == '__main__':
        app.run(debug=True,host='192.168.213.15',port=28222)

    四、新建service目录与a.py

    class AClass:  
        def add(self):  
            return "ok"

    五、启动服务

    python ./app.py

    六、访问服务

    curl http://192.168.213.15:28222/
  • 相关阅读:
    BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊(分块)
    BZOJ 2648 SJY摆棋子(KD Tree)
    Codeforces Round #441 D. Sorting the Coins(模拟)
    HDU 3400 Line belt (三分套三分)
    HDU 5919 Sequence II(主席树+区间不同数个数+区间第k小)
    HDU 5985 Lucky Coins(概率)
    HDU 5988 Coding Contest(浮点数费用流)
    HDU 5792 World is Exploding(树状数组+离散化)
    HDU 5791 Two(LCS求公共子序列个数)
    HDU 5787 K-wolf Number(数位dp)
  • 原文地址:https://www.cnblogs.com/Javame/p/9316717.html
Copyright © 2011-2022 走看看