Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

access.py 2.3KB

1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import datetime
  2. class AccessLogGenerator:
  3. """
  4. Object that implements the Daphne "action logger" internal interface in
  5. order to provide an access log in something resembling NCSA format.
  6. """
  7. def __init__(self, stream):
  8. self.stream = stream
  9. def __call__(self, protocol, action, details):
  10. """
  11. Called when an action happens; use it to generate log entries.
  12. """
  13. # HTTP requests
  14. if protocol == "http" and action == "complete":
  15. self.write_entry(
  16. host=details["client"],
  17. date=datetime.datetime.now(),
  18. request="%(method)s %(path)s" % details,
  19. status=details["status"],
  20. length=details["size"],
  21. )
  22. # Websocket requests
  23. elif protocol == "websocket" and action == "connecting":
  24. self.write_entry(
  25. host=details["client"],
  26. date=datetime.datetime.now(),
  27. request="WSCONNECTING %(path)s" % details,
  28. )
  29. elif protocol == "websocket" and action == "rejected":
  30. self.write_entry(
  31. host=details["client"],
  32. date=datetime.datetime.now(),
  33. request="WSREJECT %(path)s" % details,
  34. )
  35. elif protocol == "websocket" and action == "connected":
  36. self.write_entry(
  37. host=details["client"],
  38. date=datetime.datetime.now(),
  39. request="WSCONNECT %(path)s" % details,
  40. )
  41. elif protocol == "websocket" and action == "disconnected":
  42. self.write_entry(
  43. host=details["client"],
  44. date=datetime.datetime.now(),
  45. request="WSDISCONNECT %(path)s" % details,
  46. )
  47. def write_entry(
  48. self, host, date, request, status=None, length=None, ident=None, user=None
  49. ):
  50. """
  51. Writes an NCSA-style entry to the log file (some liberty is taken with
  52. what the entries are for non-HTTP)
  53. """
  54. self.stream.write(
  55. '%s %s %s [%s] "%s" %s %s\n'
  56. % (
  57. host,
  58. ident or "-",
  59. user or "-",
  60. date.strftime("%d/%b/%Y:%H:%M:%S"),
  61. request,
  62. status or "-",
  63. length or "-",
  64. )
  65. )