码迷,mamicode.com
首页 > 编程语言 > 详细

Python SimpleHTTPServer 简单开发

时间:2015-10-19 17:40:47      阅读:382      评论:0      收藏:0      [点我收藏+]

标签:

目前就职的公司,架构一直没有稳定下来。运维相关工具也没有完善,有的开发没有登入测试环境服务器的权限,所以就自己写个自动更新测试环境的工具。因为服务器的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 &




Python SimpleHTTPServer 简单开发

标签:

原文地址:http://my.oschina.net/jackin/blog/518940

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!