教你分分钟搞定Python之Flask框架

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

教你分分钟搞定Python之Flask框架

一、安装pip

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

二、下载Flask模块

pip install flask

三、新建app.py文件

#!flask/bin/python
# -*- coding: utf- -*-
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=)

四、新建service目录与a.py

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

五、启动服务

python ./app.py

六、访问服务

curl http://192.168.213.15:28222/
上一篇:Python中的 @staticmethod@classmethod方法


下一篇:RunTime(运行时机制)