Skip to content

Instantly share code, notes, and snippets.

@davidbgk
Forked from karlcow/webmention.py
Last active July 4, 2022 01:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidbgk/5469846 to your computer and use it in GitHub Desktop.
Save davidbgk/5469846 to your computer and use it in GitHub Desktop.
Using gunicorn
def server_app(environ, start_response):
method = environ.get('REQUEST_METHOD', 'GET')
if method == 'GET':
return get_response(start_response)
elif method == 'POST':
accept_header = environ.get('HTTP_ACCEPT', '*/*')
post_data = environ.get('wsgi.input').read()
return post_response(start_response, accept_header, post_data)
def get_response(start_response):
data = "Only POST method is allowed. See http://webmention.org/"
start_response("405", [
("Content-Type", "text/plain"),
("Content-Length", str(len(data)))
])
return iter([data])
def post_response(start_response, accept_header, post_data):
print accept_header
if accept_header in ('application/json', '*/*'):
return json_response(start_response, post_data)
elif accept_header in ('text/html',):
return html_response(start_response, post_data)
else:
data = ("Wrong Accept header. We can provide only text/html or"
" application/json. See http://webmention.org/\n"
"Accept header is %s" % accept_header)
start_response("406", [
("Content-Type", "text/plain"),
("Content-Length", str(len(data)))
])
return iter([data])
def json_response(start_response, post_data):
data = '{"result": "WebMention was successful"}'
start_response("202", [
("Content-Type", "application/json"),
("Content-Length", str(len(data)))
])
return iter([data])
def html_response(start_response, post_data):
data = """<!DOCTYPE html>
<html lang="en">
<head>
<title>WebMention</title>
</head>
<body>
<p><a href="http://webmention.org/">WebMention</a> was successful</p>
</body>
</html>"""
start_response("202", [
("Content-Type", "text/html"),
("Content-Length", str(len(data)))
])
return iter([data])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment