diff --git a/sanic/app.py b/sanic/app.py index eb35e9ff..46ba8cd5 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -2,13 +2,15 @@ import logging import logging.config import os import re +import typing import warnings -from asyncio import CancelledError, ensure_future, get_event_loop +from asyncio import CancelledError, ensure_future, get_event_loop, Protocol from collections import defaultdict, deque from functools import partial from inspect import getmodulename, isawaitable, signature, stack -from ssl import Purpose, create_default_context +from socket import socket +from ssl import Purpose, create_default_context, SSLContext from traceback import format_exc from urllib.parse import urlencode, urlunparse @@ -967,34 +969,46 @@ class Sanic: def run( self, - host=None, - port=None, - debug=False, - ssl=None, - sock=None, - workers=1, - protocol=None, - backlog=100, - stop_event=None, - register_sys_signals=True, - access_log=None, - **kwargs - ): + host: typing.Optional[str]=None, + port: typing.Optional[int]=None, + debug: bool=False, + ssl: typing.Union[dict, SSLContext, None]=None, + sock: typing.Optional[socket]=None, + workers: int=1, + protocol: typing.Type[Protocol]=None, + backlog: int=100, + stop_event: typing.Any=None, + register_sys_signals: bool=True, + access_log: bool=None, + **kwargs: typing.Any + ) -> None: """Run the HTTP Server and listen until keyboard interrupt or term signal. On termination, drain connections before closing. :param host: Address to host on + :type host: str :param port: Port to host on + :type port: int :param debug: Enables debug output (slows server) + :type debug: bool :param ssl: SSLContext, or location of certificate and key for SSL encryption of worker(s) + :type ssl:SSLContext or dict :param sock: Socket for the server to accept connections from + :type sock: socket :param workers: Number of processes received before it is respected + :type workers: int + :param protocol: Subclass of asyncio Protocol class + :type protocol: type[Protocol] :param backlog: a number of unaccepted connections that the system will allow before refusing new connections - :param stop_event: event to be triggered before stopping the app + :type backlog: int + :param stop_event: event to be triggered before stopping the app - deprecated + :type stop_event: None :param register_sys_signals: Register SIG* events - :param protocol: Subclass of asyncio protocol class + :type register_sys_signals: bool + :param access_log: Enables writing access logs (slows server) + :type access_log: bool :return: Nothing """ if "loop" in kwargs: @@ -1085,16 +1099,16 @@ class Sanic: async def create_server( self, - host=None, - port=None, - debug=False, - ssl=None, - sock=None, - protocol=None, - backlog=100, - stop_event=None, - access_log=None, - ): + host: typing.Optional[str] = None, + port: typing.Optional[int] = None, + debug: bool = False, + ssl: typing.Union[dict, SSLContext, None] = None, + sock: typing.Optional[socket] = None, + protocol: typing.Type[Protocol] = None, + backlog: int = 100, + stop_event: typing.Any = None, + access_log: bool = None, + ) -> None: """ Asynchronous version of :func:`run`. @@ -1105,6 +1119,28 @@ class Sanic: .. note:: This does not support multiprocessing and is not the preferred way to run a :class:`Sanic` application. + + :param host: Address to host on + :type host: str + :param port: Port to host on + :type port: int + :param debug: Enables debug output (slows server) + :type debug: bool + :param ssl: SSLContext, or location of certificate and key + for SSL encryption of worker(s) + :type ssl:SSLContext or dict + :param sock: Socket for the server to accept connections from + :type sock: socket + :param protocol: Subclass of asyncio Protocol class + :type protocol: type[Protocol] + :param backlog: a number of unaccepted connections that the system + will allow before refusing new connections + :type backlog: int + :param stop_event: event to be triggered before stopping the app - deprecated + :type stop_event: None + :param access_log: Enables writing access logs (slows server) + :type access_log: bool + :return: Nothing """ if sock is None: diff --git a/sanic/config.py b/sanic/config.py index 5214f8f5..f1c6c26f 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -1,5 +1,6 @@ import os import types +from distutils.util import strtobool from sanic.exceptions import PyFileError @@ -124,7 +125,7 @@ class Config(dict): try: self[config_key] = float(v) except ValueError: - if v in ["True", "False"]: - self[config_key] = v == "True" - else: + try: + self[config_key] = bool(strtobool(v)) + except ValueError: self[config_key] = v