Skip to content

Instantly share code, notes, and snippets.

@santrancisco
Last active May 22, 2019 04:32
Show Gist options
  • Save santrancisco/11cd6c275d5ed6cb92c2f52ea6143d05 to your computer and use it in GitHub Desktop.
Save santrancisco/11cd6c275d5ed6cb92c2f52ea6143d05 to your computer and use it in GitHub Desktop.
Simple httpserver in python to answer to slack challenge for event subscription
#!/usr/bin/env python
"""
Very simple HTTP server in python to answer challenge request from slack server when create an event subscription
Usage::
./dummy-web-server.py [<port>]
Send a GET request::
curl http://localhost
Send a HEAD request::
curl -I http://localhost
Send a POST request::
curl -d "foo=bar&bin=baz" http://localhost
"""
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer
import json
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
self._set_headers()
self.wfile.write("<html><body><h1>hi!</h1></body></html>")
def do_HEAD(self):
self._set_headers()
def do_POST(self):
# Doesn't do anything with posted data
self._set_headers()
content_len = int(self.headers.getheader('content-length', 0))
post_body = self.rfile.read(content_len)
parsed_body = json.loads(post_body)
self.wfile.write('{"challenge":"'+parsed_body['challenge']+'"}')
def run(server_class=HTTPServer, handler_class=S, port=9000):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print 'Starting httpd...'
httpd.serve_forever()
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment