如何在 Python 中接收 Github Webhooks
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14536992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I receive Github Webhooks in Python
提问by Christoph
Github offers to send Post-receive hooksto an URL of your choice when there's activity on your repo. I want to write a smallPython command-line/background (i.e. no GUI or webapp) application running on my computer (later on a NAS), which continually listensfor those incoming POST requests, and once a POST is receivedfrom 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.
Github 提供在你的 repo 上有活动时将Post-receive hooks发送到你选择的 URL。我想编写一个在我的计算机上运行的小型Python 命令行/后台(即没有 GUI 或 webapp)应用程序(后来在 NAS 上),它不断地侦听那些传入的 POST 请求,一旦从 Github收到一个 POST ,它处理其中包含的 JSON 信息。只要我有它就处理 json 没问题。POST 可以来自 github 给出的少量 IP;我计划/希望在我的计算机上指定一个端口来发送它。
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.
问题是,我对 Web 技术了解得不够多,无法处理您在搜索时发现的大量选项……我是否使用 Django、Requests、sockets、Flask、微框架……?我不知道所涉及的大多数术语是什么意思,而且大多数听起来像是它们提供的太多/太大而无法解决我的问题 - 我只是不知所措,不知道从哪里开始。
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.
我能找到的大多数关于 POST/GET 的教程似乎都与从网站发送或直接请求数据有关,但与持续监听数据无关。
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?
我觉得这个问题并不难,一旦我知道去哪里/如何做,就会归结为几行。任何人都可以提供指针/教程/示例/示例代码吗?
采纳答案by ford
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):
这是一个基本的 web.py 示例,用于通过 POST 接收数据并对其进行处理(在这种情况下,只需将其打印到 stdout):
import web
urls = ('/.*', '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:
我使用hurl.it向它发布了一些数据(在我的路由器上转发 8080 之后),并看到以下输出:
$ python hooks.py
http://0.0.0.0:8080/
DATA RECEIVED:
test=thisisatest&test2=25
50.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.
您应该能够为您的 JSON 处理换出打印语句。
To specify the port number, call the script with an extra argument:
要指定端口号,请使用额外的参数调用脚本:
$ python hooks.py 1234
回答by Burhan Khalid
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.
首先,Web 是基于请求-响应的。所以有些东西会请求你的链接,你会相应地做出回应。您的服务器应用程序将持续监听一个端口;你不必担心。
Here is the similar version in Flask(my micro framework of choice):
这是Flask(我选择的微框架)中的类似版本:
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):
运行服务器(上面的代码保存在 中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:
这是对服务器的请求,基本上github会做什么:
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 -
回答by Havok
I would use:
我会用:
https://github.com/carlos-jenkins/python-github-webhooks
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:
您可以配置一个 Web 服务器来使用它,或者如果您只需要一个没有 Web 服务器的进程在那里运行,您可以启动集成服务器:
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.
迟到了,无耻的自动促销,对不起。

