Customizable protocol

This commit is contained in:
38elements 2016-12-23 00:13:38 +09:00
parent ef9d8710f5
commit c657c531b4
2 changed files with 11 additions and 8 deletions

View File

@ -12,7 +12,7 @@ from .exceptions import Handler
from .log import log, logging from .log import log, logging
from .response import HTTPResponse from .response import HTTPResponse
from .router import Router from .router import Router
from .server import serve from .server import serve, HttpProtocol
from .static import register as static_register from .static import register as static_register
from .exceptions import ServerError from .exceptions import ServerError
@ -230,14 +230,15 @@ class Sanic:
# Execution # Execution
# -------------------------------------------------------------------- # # -------------------------------------------------------------------- #
def run(self, host="127.0.0.1", port=8000, debug=False, before_start=None, def run(self, host="127.0.0.1", port=8000, protocol=HttpProtocol,
after_start=None, before_stop=None, after_stop=None, sock=None, debug=False, before_start=None, after_start=None, before_stop=None,
workers=1, loop=None): after_stop=None, sock=None, workers=1, loop=None):
""" """
Runs the HTTP Server and listens until keyboard interrupt or term Runs the HTTP Server and listens until keyboard interrupt or term
signal. On termination, drains connections before closing. signal. On termination, drains connections before closing.
:param host: Address to host on :param host: Address to host on
:param port: Port to host on :param port: Port to host on
:param protocol: Subclass of asyncio.Protocol
:param debug: Enables debug output (slows server) :param debug: Enables debug output (slows server)
:param before_start: Function to be executed before the server starts :param before_start: Function to be executed before the server starts
accepting connections accepting connections
@ -258,6 +259,7 @@ class Sanic:
self.loop = loop self.loop = loop
server_settings = { server_settings = {
'protocol': protocol,
'host': host, 'host': host,
'port': port, 'port': port,
'sock': sock, 'sock': sock,

View File

@ -221,12 +221,13 @@ def trigger_events(events, loop):
loop.run_until_complete(result) loop.run_until_complete(result)
def serve(host, port, request_handler, error_handler, before_start=None, def serve(protocol, host, port, request_handler, error_handler,
after_start=None, before_stop=None, after_stop=None, before_start=None, after_start=None, before_stop=None,
debug=False, request_timeout=60, sock=None, after_stop=None, debug=False, request_timeout=60, sock=None,
request_max_size=None, reuse_port=False, loop=None): request_max_size=None, reuse_port=False, loop=None):
""" """
Starts asynchronous HTTP Server on an individual process. Starts asynchronous HTTP Server on an individual process.
:param protocol: subclass of asyncio.Protocol
:param host: Address to host on :param host: Address to host on
:param port: Port to host on :param port: Port to host on
:param request_handler: Sanic request handler with middleware :param request_handler: Sanic request handler with middleware
@ -253,7 +254,7 @@ def serve(host, port, request_handler, error_handler, before_start=None,
connections = set() connections = set()
signal = Signal() signal = Signal()
server = partial( server = partial(
HttpProtocol, protocol,
loop=loop, loop=loop,
connections=connections, connections=connections,
signal=signal, signal=signal,