python git webhook

    #!/usr/bin/python
    # coding=utf8
    
    from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
    import commands
    import json
    
    PORT_NUMBER = 8080
    WEB_ROOT = '/www_root/web_root'
    PULL_COMMAND = ''.join(['cd ', WEB_ROOT, ' && git pull'])
    
    
    class myHandler(BaseHTTPRequestHandler):
        def do_GET(self):
            if self.path == "/git/pull":
                self.git_pull()
                return
    
            if self.path == "/test":
                self.send_response(200)
                self.send_header('Content-type', "application/json")
                self.end_headers()
                self.wfile.write("service is runing!")
                return
    
        def do_POST(self):
            if self.path == "/git/pull":
                self.git_pull()
                return
    
        def git_pull(self):
            (status, output) = commands.getstatusoutput(PULL_COMMAND)
            obj = [{'status': status, 'output': output}]
            encodedjson = json.dumps(obj)
            self.send_response(200)
            self.send_header('Content-type', "application/json")
            self.end_headers()
            self.wfile.write(encodedjson)
    
    try:
        server = HTTPServer(('', PORT_NUMBER), myHandler)
        print 'Started httpserver on port ', PORT_NUMBER
    
        server.serve_forever()
    
    except KeyboardInterrupt:
        print '^C received, shutting down the web server'
        server.socket.close()

标签: git, webhook, python, linux