Repository with sources and generator of https://larlet.fr/david/ https://larlet.fr/david/
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.py 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/python3
  2. import cgi
  3. import cgitb
  4. import os
  5. import subprocess
  6. import urllib.request
  7. cgitb.enable()
  8. form = cgi.FieldStorage()
  9. source = form.getvalue("source", None)
  10. target = form.getvalue("target", None)
  11. server = os.environ.get("SERVER_NAME", None)
  12. method = os.environ.get("REQUEST_METHOD", "GET")
  13. def validate(source, target, server, method):
  14. status = 400
  15. if method != "POST":
  16. message = "Webmention MUST be performed through a POST request."
  17. elif source is None or target is None:
  18. message = "Please fill in the source and target fields."
  19. elif "://" not in source or "://" not in target:
  20. message = "Source and target fields should be URLs."
  21. elif not target.split("://", 1)[1].startswith(server):
  22. message = "Target should be a link to this domain: {0}".format(server)
  23. elif target not in urllib.request.urlopen(source).read().decode("utf-8"):
  24. message = "Source should have a link to the target: {0}".format(target)
  25. else:
  26. message = (
  27. "Success! With source={source} and target={target}."
  28. '<a href="{target}">Back to the target article</a>.'
  29. ).format(
  30. source=source,
  31. target=target,
  32. )
  33. status = 202
  34. return status, message, source, target
  35. def send_response(callback, status, message, source, target):
  36. print("Content-type: text/html")
  37. print("Status: {0}".format(status))
  38. print()
  39. print(
  40. """<!doctype html><html lang=en>
  41. <head><meta charset=utf-8><title>Webmention</title></head>
  42. <body><p>{message}</p></body></html>""".format(
  43. message=message
  44. )
  45. )
  46. if status == 202:
  47. callback(source, target)
  48. def on_valid_submission(source, target):
  49. commands = [
  50. "cd /home/larlet/www",
  51. "source /home/larlet/.virtualenvs/webmention/bin/activate",
  52. "fab --hide=everything add_webmention:{source},{target}".format(
  53. source=source,
  54. target=target,
  55. ),
  56. ]
  57. subprocess.call(" && ".join(commands), shell=True)
  58. send_response(on_valid_submission, *validate(source, target, server, method))