Python接收开源中国GITOSC WebHook的POST数据

# coding:utf-8
import urllib
import json
from BaseHTTPServer import HTTPServer,BaseHTTPRequestHandler

class RequestHandler(BaseHTTPRequestHandler):
    def _writeheaders(self):
        print self.path
        print self.headers
        self.send_response(200);
        self.send_header('Content-type','text/html');
        self.end_headers()
    def do_Head(self):
        self._writeheaders()
    def do_GET(self):
        self._writeheaders()
        self.wfile.write("""""" str(self.headers))
    def do_POST(self):
        self._writeheaders()
        length = self.headers.getheader('content-length');
        nbytes = int(length)
        data = self.rfile.read(nbytes)
        self.wfile.write('ok')
        #print data
        data = urllib.unquote(data)
        data = data[5:]
        #print data
        json_obj = json.loads(data)
        commits_cnt = len(json_obj['push_data']['commits'])
        print commits_cnt
        for i in range(commits_cnt):
            print i
            print json_obj['push_data']['commits'][i]['message']
if __name__=="__main__":
    addr = ('',8765)
    server = HTTPServer(addr,RequestHandler)
    server.serve_forever()


你可能感兴趣的:(json,python,webhook,post)