src.myserver.log

A package for learning network programming in Python.

This module (file) manages the HTTP logging messages.

  1######################################################################
  2# Copyright (c) Adrien Luxey-Bitri, Boris Baldassari
  3#
  4# This program and the accompanying materials are made
  5# available under the terms of the Eclipse Public License 2.0
  6# which is available at https://www.eclipse.org/legal/epl-2.0/
  7#
  8# SPDX-License-Identifier: EPL-2.0
  9######################################################################
 10
 11"""
 12A package for learning network programming in Python.
 13
 14This module (file) manages the HTTP logging messages.
 15"""
 16
 17from myserver.date import now_rfc2616
 18
 19
 20def log(msg: str):
 21    """
 22    Logs a message to stdout, with a timestamp.
 23    
 24    Output is: `timestamp - message`.
 25    
 26    Parameters
 27    ----------
 28    - msg : str 
 29        The message string to print.
 30    """
 31    _time = now_rfc2616()
 32    print(f"{_time} - {msg}")
 33
 34def log_address(addr: tuple[str, int], msg: str):
 35    """
 36    Logs a message to stdout, with a timestamp and an address (host:port).
 37    
 38    Output is: `timestamp - host:port - message`.
 39    
 40    Parameters
 41    ----------
 42    - addr: tuple[str, int]
 43        The address to print, as a tuple (host, port)
 44    - msg: str
 45        The message string to print.
 46    """
 47    _addr = f"{addr[0]}:{addr[1]}"
 48    log(f"{_addr} - {msg}")
 49
 50def log_request(addr: tuple[str, int], req: dict[str, dict]):
 51    """
 52    Logs a request message to stdout, with a timestamp and an address (host:port).
 53    If the User-Agent header is passed, its value is appended at the end.
 54    
 55    Output is: `timestamp - host:port - verb resource`.
 56
 57    Output with User-Agent is: `timestamp - host:port - verb resource - user_agent`.
 58
 59    Parameters
 60    ----------
 61    - addr: tuple[str, int]
 62        The address to print, as a tuple (host, port)
 63    - req: dict[str, dict]
 64        The request to print.
 65    """
 66    assert 'head' in req \
 67        and 'verb' in req['head'] and 'resource' in req['head']
 68    
 69    verb = req['head']['verb']
 70    res = req['head']['resource']
 71    msg = f"{verb} {res}"
 72
 73    if 'params' in req and 'User-Agent' in req['params']:
 74        msg += f" - {req['params']['User-Agent']}"
 75
 76    log_address(addr, msg)
 77
 78def log_reply(addr: tuple[str, int], req: dict[str, dict], code: int):
 79    """
 80    Logs an HTTP reply to stdout, with timestamp, address (host:port), code.
 81    If the User-Agent header is passed, its value is appended at the end.
 82    
 83    Output is: `timestamp - host:port - HTTP-verb HTTP-resource - code`.
 84
 85    Output with User-Agent is: `timestamp - host:port - HTTP-verb HTTP-resource - code - user_agent`.
 86
 87    Parameters
 88    ----------
 89    - addr: tuple[str, int]
 90        The address to print, as a tuple (host, port)
 91    - req: dict[str, dict]
 92        The request to print.
 93    - code: int
 94        The replied code to print.
 95    """
 96    assert 'head' in req \
 97        and 'verb' in req['head'] and 'resource' in req['head']
 98    
 99    verb = req['head']['verb']
100    res = req['head']['resource']
101    msg = f"{verb} {res} - {code}"
102
103    if 'params' in req and 'User-Agent' in req['params']:
104        msg += f" - {req['params']['User-Agent']}"
105
106    log_address(addr, msg)
def log(msg: str):
21def log(msg: str):
22    """
23    Logs a message to stdout, with a timestamp.
24    
25    Output is: `timestamp - message`.
26    
27    Parameters
28    ----------
29    - msg : str 
30        The message string to print.
31    """
32    _time = now_rfc2616()
33    print(f"{_time} - {msg}")

Logs a message to stdout, with a timestamp.

Output is: timestamp - message.

Parameters

  • msg : str The message string to print.
def log_address(addr: tuple[str, int], msg: str):
35def log_address(addr: tuple[str, int], msg: str):
36    """
37    Logs a message to stdout, with a timestamp and an address (host:port).
38    
39    Output is: `timestamp - host:port - message`.
40    
41    Parameters
42    ----------
43    - addr: tuple[str, int]
44        The address to print, as a tuple (host, port)
45    - msg: str
46        The message string to print.
47    """
48    _addr = f"{addr[0]}:{addr[1]}"
49    log(f"{_addr} - {msg}")

Logs a message to stdout, with a timestamp and an address (host:port).

Output is: timestamp - host:port - message.

Parameters

  • addr: tuple[str, int] The address to print, as a tuple (host, port)
  • msg: str The message string to print.
def log_request(addr: tuple[str, int], req: dict[str, dict]):
51def log_request(addr: tuple[str, int], req: dict[str, dict]):
52    """
53    Logs a request message to stdout, with a timestamp and an address (host:port).
54    If the User-Agent header is passed, its value is appended at the end.
55    
56    Output is: `timestamp - host:port - verb resource`.
57
58    Output with User-Agent is: `timestamp - host:port - verb resource - user_agent`.
59
60    Parameters
61    ----------
62    - addr: tuple[str, int]
63        The address to print, as a tuple (host, port)
64    - req: dict[str, dict]
65        The request to print.
66    """
67    assert 'head' in req \
68        and 'verb' in req['head'] and 'resource' in req['head']
69    
70    verb = req['head']['verb']
71    res = req['head']['resource']
72    msg = f"{verb} {res}"
73
74    if 'params' in req and 'User-Agent' in req['params']:
75        msg += f" - {req['params']['User-Agent']}"
76
77    log_address(addr, msg)

Logs a request message to stdout, with a timestamp and an address (host:port). If the User-Agent header is passed, its value is appended at the end.

Output is: timestamp - host:port - verb resource.

Output with User-Agent is: timestamp - host:port - verb resource - user_agent.

Parameters

  • addr: tuple[str, int] The address to print, as a tuple (host, port)
  • req: dict[str, dict] The request to print.
def log_reply(addr: tuple[str, int], req: dict[str, dict], code: int):
 79def log_reply(addr: tuple[str, int], req: dict[str, dict], code: int):
 80    """
 81    Logs an HTTP reply to stdout, with timestamp, address (host:port), code.
 82    If the User-Agent header is passed, its value is appended at the end.
 83    
 84    Output is: `timestamp - host:port - HTTP-verb HTTP-resource - code`.
 85
 86    Output with User-Agent is: `timestamp - host:port - HTTP-verb HTTP-resource - code - user_agent`.
 87
 88    Parameters
 89    ----------
 90    - addr: tuple[str, int]
 91        The address to print, as a tuple (host, port)
 92    - req: dict[str, dict]
 93        The request to print.
 94    - code: int
 95        The replied code to print.
 96    """
 97    assert 'head' in req \
 98        and 'verb' in req['head'] and 'resource' in req['head']
 99    
100    verb = req['head']['verb']
101    res = req['head']['resource']
102    msg = f"{verb} {res} - {code}"
103
104    if 'params' in req and 'User-Agent' in req['params']:
105        msg += f" - {req['params']['User-Agent']}"
106
107    log_address(addr, msg)

Logs an HTTP reply to stdout, with timestamp, address (host:port), code. If the User-Agent header is passed, its value is appended at the end.

Output is: timestamp - host:port - HTTP-verb HTTP-resource - code.

Output with User-Agent is: timestamp - host:port - HTTP-verb HTTP-resource - code - user_agent.

Parameters

  • addr: tuple[str, int] The address to print, as a tuple (host, port)
  • req: dict[str, dict] The request to print.
  • code: int The replied code to print.