Python SimpleHTTPServer 简单开发

目前就职的公司,架构一直没有稳定下来。运维相关工具也没有完善,有的开发没有登入测试环境服务器的权限,所以就自己写个自动更新测试环境的工具。因为服务器的Python 版本为 python 2.6.6

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>autoRelease</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<form method="post" action="/" id="update">
    <p><label>环境:<input name="environ" type="text" value="test"/></label></p>

    <p><label>分支:<input name="branch" type="text"/></label></p>

    <p><input type="submit" value="更新"/></p>
</form>
<script type="text/javascript">
    (function ($) {
        $(document).ready(function () {
            $('#update').submit(function (e) {
                e.preventDefault();
                $.post('/', $(this).serialize(), function (response) {
                    console.log(response);
                }, 'json');
                return false;
            });
        });
    })(jQuery)
</script>
</body>
</html>



Python 代码:

import SimpleHTTPServer
import SocketServer
import logging
import cgi
import subprocess


class AutoReleaseRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        logging.warning("======= GET STARTED =======")
        logging.warning(self.headers)
        self.path = '/index.html'
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

    def do_POST(self):
        logging.warning("======= POST STARTED =======")
        logging.warning(self.headers)
        form = cgi.FieldStorage(
            fp=self.rfile,
            headers=self.headers,
            environ={'REQUEST_METHOD': 'POST',
                     'CONTENT_TYPE': self.headers['Content-Type'],
            })
        logging.warning("======= POST VALUES =======")
        self.wfile.write('{"status":"OK"')
        for item in form.list:
            logging.warning(item)
            self.wfile.write(',"%s":"%s"\n' % (item.name, item.value))
        command = 'cd /home/www/%s/; git stash;git pull --rebase  origin %s ' % (
            form['environ'].value, form['branch'].value)
        self.wfile.write(',"command":"%s"' % command)
        self.wfile.write('}')
        subprocess.Popen(command, stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT, shell=True)


logging.warning("\n")

if __name__ == '__main__':
    print 'Starting server, use <Ctrl-C> to stop'
    server = SocketServer.TCPServer(('0.0.0.0', 1024), AutoReleaseRequestHandler)
    server.serve_forever()



运行的命令:
nohup python release.py > /dev/null &




转载于:https://my.oschina.net/jackin/blog/518940

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值