`
bee1314
  • 浏览: 163651 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

bottle简单实现一个调用shell的web前端

 
阅读更多

    有一个small case,一个简单的web界面填写两个参数,能给传递给后端,然后后端调用shell,然后将前端填写的参数传递给shell。 对于这个简单的需求,首先python搞定,本打算用tornado搞定,但想到之前学习了下bottle,简单优雅,更可以简单的解决这个问题。然后就选择web framework。python调用shell也很简单,但是有个更优雅的python的sh交互的库,就是sh,可以通过pip install sh来安装,sh真是不错,昨天看到了一个clojure的sh的DSL,发现真是异曲同工之妙。后面单独介绍下。

 

创建项目环境:

pip install mkvirtualenvwrapper
mkvirtualenv bottle-env
workon bottle-env

 项目依赖:

argparse==1.2.1
bottle==0.11.6
sh==1.09
wsgiref==0.1.2

项目结构:
├── app.py
├── LICENSE
├── README.md
├── requirement.txt
└── views
    ├── index.tpl
    └── result.tpl

 

   简单的app.py 简单的web层

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sh
from bottle import get, post, template, request, run


#GET/
@get('/')
def index():
    return template('index.tpl')


@post('/shell')
def shell():
    param1 = request.forms.get('param1')
    param2 = request.forms.get('param2')
    print "param1:%s param2:%s" % (param1, param2)
    #call you shell, for example ls
    #the sh libary is so awesome!!
    #if you need call local sh, you can
    #run_shell = sh.Command('/home/run.sh')
    #run_shell()
    result = sh.ls(param1 + param2)
    print "end..."
    return template('result.tpl', result=result)


if __name__ == '__main__':
    run(host='0.0.0.0', port=3000, debug=True, reloader=True)

 

code:https://github.com/hufeng/simplesh

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics