Skip to content

Instantly share code, notes, and snippets.

@davidbgk davidbgk/
Last active Jan 1, 2017

What would you like to do?
Implementation of Webmention in Python 3 as a CGI script.
import os
import cgi
import cgitb
import subprocess
import urllib.request
form = cgi.FieldStorage()
source = form.getvalue('source', None)
target = form.getvalue('target', None)
server = os.environ.get('SERVER_NAME', None)
method = os.environ.get('REQUEST_METHOD', 'GET')
def validate(source, target, server, method, callback_if_valid):
status = 400
if method != 'POST':
message = 'Webmention MUST be performed through a POST request.'
elif source is None or target is None:
message = 'Please fill in the source and target fields.'
elif '://' not in source or '://' not in target:
message = 'Source and target fields should be URLs.'
elif not target.split('://', 1)[1].startswith(server):
message = 'Target should be a link to this domain: {0}'.format(server)
elif not target in urllib.request.urlopen(source).read().decode('utf-8'):
message = 'Source should have a link to the target: {0}'.format(target)
message = 'Success! With source={source} and target={target}'.format(
status = 202
callback_if_valid(source, target)
return status, message
def send_response(status, message):
print('Content-type: text/html')
print('Status: {0}'.format(status))
print("""<!doctype html><html lang=en>
<head><meta charset=utf-8><title>Webmention</title></head>
def on_valid_submission(source, target):
commands = [
'cd /home/larlet/www',
'source /home/larlet/.virtualenvs/webmention/bin/activate',
'fab --hide=everything add_webmention:{source},{target}'.format(
]' && '.join(commands), shell=True)
send_response(*validate(source, target, server, method, on_valid_submission))

This comment has been minimized.

Copy link
Owner Author

davidbgk commented Dec 5, 2016

/poke @fcotton :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.