Skip to main content

ASGIHandler

Handler for ASGI requests.

Attributes

AttributeTypeDescription
request_classclass = ASGIRequestThe class used to instantiate the request object from the ASGI scope and body file.
chunk_sizeint = 65536Size to chunk response bodies into for multiple response messages.

Constructor

Signature

def ASGIHandler() - > null

Methods


handle()

@classmethod
def handle(
scope: dict,
receive: callable,
send: callable
) - > None

Handles the ASGI request. Called via the call method.

Parameters

NameTypeDescription
scopedictThe ASGI connection scope dictionary
receivecallableThe ASGI receive channel
sendcallableThe ASGI send channel

Returns

TypeDescription
Nonenull

listen_for_disconnect()

@classmethod
def listen_for_disconnect(
receive: callable
) - > None

Listen for disconnect from the client.

Parameters

NameTypeDescription
receivecallableThe ASGI receive channel used to monitor for client disconnection events

Returns

TypeDescription
Nonenull

run_get_response()

@classmethod
def run_get_response(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > [HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Get async response.

Parameters

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The Django request object to be processed by the middleware and view layer

Returns

TypeDescription
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The processed Django response object with handler metadata attached

read_body()

@classmethod
def read_body(
receive: callable
) - > SpooledTemporaryFile

Reads an HTTP body from an ASGI connection.

Parameters

NameTypeDescription
receivecallableThe ASGI receive channel used to stream request body chunks

Returns

TypeDescription
SpooledTemporaryFileA file-like object containing the full request body, buffered in memory or on disk

create_request()

@classmethod
def create_request(
scope: dict,
body_file: file-like object
) - > tuple

Create the Request object and returns either (request, None) or (None, response) if there is an error response.

Parameters

NameTypeDescription
scopedictThe ASGI connection scope
body_filefile-like objectThe file-like object containing the request body data

Returns

TypeDescription
tupleA tuple containing either the ASGIRequest and None, or None and an error HttpResponse

handle_uncaught_exception()

@classmethod
def handle_uncaught_exception(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest),
resolver: [URLResolver](../../../urls/resolvers/urlresolver.md?sid=django_urls_resolvers_urlresolver),
exc_info: tuple
) - > [HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Last-chance handler for exceptions.

Parameters

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The request object that was being processed when the exception occurred
resolver[URLResolver](../../../urls/resolvers/urlresolver.md?sid=django_urls_resolvers_urlresolver)The URL resolver used to find an error handler view
exc_infotupleThe exception information tuple as returned by sys.exc_info()

Returns

TypeDescription
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)A server error response, either formatted by the base handler or a plain text fallback

send_response()

@classmethod
def send_response(
response: [HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse),
send: callable
) - > None

Encode and send a response out over ASGI.

Parameters

NameTypeDescription
response[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The Django response object to be serialized and sent
sendcallableThe ASGI send channel used to transmit response messages

Returns

TypeDescription
Nonenull

chunk_bytes()

@classmethod
def chunk_bytes(
data: bytes
) - > generator

Chunks some data up so it can be sent in reasonable size messages. Yields (chunk, last_chunk) tuples.

Parameters

NameTypeDescription
databytesThe raw byte string to be split into smaller chunks for transmission

Returns

TypeDescription
generatorA generator yielding tuples of (byte_chunk, is_last_chunk)