How do I receive Github Webhooks in Python
First thing is, web is request-response based. So something will request your link, and you will respond accordingly. Your server application will be continuously listening on a port; that you don't have to worry about.
Here is the similar version in Flask
(my micro framework of choice):
from flask import Flask, requestimport jsonapp = Flask(__name__)@app.route('/',methods=['POST'])def foo(): data = json.loads(request.data) print "New commit by: {}".format(data['commits'][0]['author']['name']) return "OK"if __name__ == '__main__': app.run()
Here is a sample run, using the example from github:
Running the server (the above code is saved in sample.py
):
burhan@lenux:~$ python sample.py * Running on http://127.0.0.1:5000/
Here is a request to the server, basically what github will do:
burhan@lenux:~$ http POST http://127.0.0.1:5000 < sample.jsonHTTP/1.0 200 OKContent-Length: 2Content-Type: text/html; charset=utf-8Date: Sun, 27 Jan 2013 19:07:56 GMTServer: Werkzeug/0.8.3 Python/2.7.3OK # <-- this is the response the client gets
Here is the output at the server:
New commit by: Chris Wanstrath127.0.0.1 - - [27/Jan/2013 22:07:56] "POST / HTTP/1.1" 200 -
Here's a basic web.py example for receiving data via POST and doing something with it (in this case, just printing it to stdout):
import weburls = ('/.*', 'hooks')app = web.application(urls, globals())class hooks: def POST(self): data = web.data() print print 'DATA RECEIVED:' print data print return 'OK'if __name__ == '__main__': app.run()
I POSTed some data to it using hurl.it (after forwarding 8080 on my router), and saw the following output:
$ python hooks.py http://0.0.0.0:8080/DATA RECEIVED: test=thisisatest&test2=2550.19.170.198:33407 - - [27/Jan/2013 10:18:37] "HTTP/1.1 POST /hooks" - 200 OK
You should be able to swap out the print statements for your JSON processing.
To specify the port number, call the script with an extra argument:
$ python hooks.py 1234
I would use:
https://github.com/carlos-jenkins/python-github-webhooks
You can configure a web server to use it, or if you just need a process running there without a web server you can launch the integrated server:
python webhooks.py
This will allow you to do everything you said you need. It, nevertheless, requires a bit of setup in your repository and in your hooks.
Late to the party and shameless autopromotion, sorry.