whr.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. import BaseHTTPServer
  4. import sys
  5. import time
  6. import urlparse
  7. import json
  8. HOST_NAME = sys.argv[1]
  9. PORT_NUMBER = int(sys.argv[2])
  10. def handle_hook(payload):
  11. pass
  12. class HookHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  13. server_version = "HookHandler/0.1"
  14. def do_GET(s):
  15. s.send_response(200)
  16. s.wfile.write('Hello!')
  17. def do_POST(s):
  18. # Check that the IP is within the GH ranges
  19. ## if not any(s.client_address[0].startswith(IP)
  20. ## for IP in ('192.30.252', '192.30.253', '192.30.254', '192.30.255')):
  21. ## s.send_error(403)
  22. length = int(s.headers['Content-Length'])
  23. post_data = urlparse.parse_qs(s.rfile.read(length).decode('utf-8'))
  24. payload = json.loads(post_data['payload'][0])
  25. handle_hook(payload)
  26. s.send_response(200)
  27. if __name__ == '__main__':
  28. server_class = BaseHTTPServer.HTTPServer
  29. httpd = server_class((HOST_NAME, PORT_NUMBER), HookHandler)
  30. print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
  31. try:
  32. httpd.serve_forever()
  33. except KeyboardInterrupt:
  34. pass
  35. httpd.server_close()
  36. print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)