Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I receive Github Webhooks in Python

Github offers to send Post-receive hooks to an URL of your choice when there's activity on your repo. I want to write a small Python command-line/background (i.e. no GUI or webapp) application running on my computer (later on a NAS), which continually listens for those incoming POST requests, and once a POST is received from Github, it processes the JSON information contained within. Processing the json as soon as I have it is no problem. The POST can come from a small number of IPs given by github; I plan/hope to specify a port on my computer where it should get sent.

The problem is, I don't know enough about web technologies to deal with the vast number of options you find when searching.. do I use Django, Requests, sockets,Flask, microframeworks...? I don't know what most of the terms involved mean, and most sound like they offer too much/are too big to solve my problem - I'm simply overwhelmed and don't know where to start.

Most tutorials about POST/GET I could find seem to be concerned with either sending or directly requesting data from a website, but not with continually listening for it.

I feel the problem is not really a difficult one, and will boil down to a couple of lines, once I know where to go/how to do it. Can anybody offer pointers/tutorials/examples/sample code?

like image 869
Christoph Avatar asked Jan 26 '13 12:01

Christoph


People also ask

How do I receive data from webhooks?

With webhooks, it's generally a three-step process: Get the webhook URL from the application you want to send data to. Use that URL in the webhook section of the application you want to receive data from. Choose the type of events you want the application to notify you about.

How do webhooks work in Python?

A webhook can be thought of as a type of API that is driven by events rather than requests. Instead of one application making a request to another to receive a response, a webhook is a service that allows one program to send data to another as soon as a particular event takes place.


1 Answers

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, request import json  app = 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.json HTTP/1.0 200 OK Content-Length: 2 Content-Type: text/html; charset=utf-8 Date: Sun, 27 Jan 2013 19:07:56 GMT Server: Werkzeug/0.8.3 Python/2.7.3  OK # <-- this is the response the client gets 

Here is the output at the server:

New commit by: Chris Wanstrath 127.0.0.1 - - [27/Jan/2013 22:07:56] "POST / HTTP/1.1" 200 - 
like image 138
Burhan Khalid Avatar answered Sep 17 '22 19:09

Burhan Khalid