Warning

These documents are OUTDATED as of 2023-12-31.

Please refer to the latest version of the documentation at sanic.dev.

Utility๏ƒ

sanic.compat๏ƒ

class sanic.compat.Header๏ƒ

Bases: CIMultiDict

Container used for both request and response headers. It is a subclass of [CIMultiDict](https://multidict.readthedocs.io/en/stable/multidict.html#cimultidictproxy)

It allows for multiple values for a single key in keeping with the HTTP spec. Also, all keys are case in-sensitive.

Please checkout [the MultiDict documentation](https://multidict.readthedocs.io/en/stable/multidict.html#multidict) for more details about how to use the object. In general, it should work very similar to a regular dictionary.

get_all(key)๏ƒ

Convenience method mapped to getall().

Parameters:

key (str) โ€“

enum sanic.compat.StrEnum(value)๏ƒ

Bases: str, Enum

An enumeration.

Member Type:

str

enum sanic.compat.UpperStrEnum(value)๏ƒ

Bases: StrEnum

Base class for string enums that are case insensitive.

Member Type:

str

sanic.compat.pypy_os_module_patch()๏ƒ

The PyPy os module is missing the โ€˜readlinkโ€™ function, which causes issues withaiofiles. This workaround replaces the missing โ€˜readlinkโ€™ function with โ€˜os.path.realpathโ€™, which serves the same purpose.

Return type:

None

sanic.compat.pypy_windows_set_console_cp_patch()๏ƒ

A patch function for PyPy on Windows that sets the console code page to UTF-8 encodingto allow for proper handling of non-ASCII characters. This function uses ctypes to call the Windows API functions SetConsoleCP and SetConsoleOutputCP to set the code page.

Return type:

None

sanic.log๏ƒ

enum sanic.log.Colors(value)๏ƒ

Bases: StrEnum

An enumeration.

Member Type:

str

Valid values are as follows:

END = <Colors.END: '\x1b[0m'>๏ƒ
BOLD = <Colors.BOLD: '\x1b[1m'>๏ƒ
BLUE = <Colors.BLUE: '\x1b[34m'>๏ƒ
GREEN = <Colors.GREEN: '\x1b[32m'>๏ƒ
PURPLE = <Colors.PURPLE: '\x1b[35m'>๏ƒ
RED = <Colors.RED: '\x1b[31m'>๏ƒ
SANIC = <Colors.SANIC: '\x1b[38;2;255;13;104m'>๏ƒ
YELLOW = <Colors.YELLOW: '\x1b[01;33m'>๏ƒ
enum sanic.log.StrEnum(value)๏ƒ

Bases: str, Enum

An enumeration.

Member Type:

str

class sanic.log.VerbosityFilter(name='')๏ƒ

Bases: Filter

filter(record)๏ƒ

Determine if the specified record is to be logged.

Returns True if the record should be logged, or False otherwise. If deemed appropriate, the record may be modified in-place.

Parameters:

record (LogRecord) โ€“

Return type:

bool

sanic.log.deprecation(message, version)๏ƒ

Add a deprecation notice

Example when a feature is being removed. In this case, version should be AT LEAST next version + 2

deprecation(โ€œHelpful messageโ€, 99.9)

Example when a feature is deprecated but not being removed:

deprecation(โ€œHelpful messageโ€, 0)

Parameters:
  • message (str) โ€“ The message of the notice

  • version (float) โ€“ The version when the feature will be removed. If it is not being removed, then set version=0.

sanic.log.LOGGING_CONFIG_DEFAULTS: Dict[str, Any] = {'disable_existing_loggers': False, 'formatters': {'access': {'class': 'logging.Formatter', 'datefmt': '[%Y-%m-%d %H:%M:%S %z]', 'format': '%(asctime)s - (%(name)s)[%(levelname)s][%(host)s]: %(request)s %(message)s %(status)s %(byte)s'}, 'generic': {'class': 'logging.Formatter', 'datefmt': '[%Y-%m-%d %H:%M:%S %z]', 'format': '%(asctime)s [%(process)s] [%(levelname)s] %(message)s'}}, 'handlers': {'access_console': {'class': 'logging.StreamHandler', 'formatter': 'access', 'stream': <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>}, 'console': {'class': 'logging.StreamHandler', 'formatter': 'generic', 'stream': <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>}, 'error_console': {'class': 'logging.StreamHandler', 'formatter': 'generic', 'stream': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>}}, 'loggers': {'sanic.access': {'handlers': ['access_console'], 'level': 'INFO', 'propagate': True, 'qualname': 'sanic.access'}, 'sanic.error': {'handlers': ['error_console'], 'level': 'INFO', 'propagate': True, 'qualname': 'sanic.error'}, 'sanic.root': {'handlers': ['console'], 'level': 'INFO'}, 'sanic.server': {'handlers': ['console'], 'level': 'INFO', 'propagate': True, 'qualname': 'sanic.server'}}, 'version': 1}๏ƒ

Defult logging configuration

sanic.log.access_logger = <Logger sanic.access (WARNING)>๏ƒ

Logger used by Sanic for access logging

sanic.log.error_logger = <Logger sanic.error (WARNING)>๏ƒ

Logger used by Sanic for error logging

sanic.log.logger = <Logger sanic.root (WARNING)>๏ƒ

General Sanic logger

sanic.log.server_logger = <Logger sanic.server (WARNING)>๏ƒ

Logger used by Sanic for server related messages