Pyjo.Message.Response - HTTP response

from Pyjo.Message.Response import Response

# Parse
res = Response()
res.parse(b'HTTP/1.0 200 OK\x0d\x0a')
res.parse(b'Content-Length: 12\x0d\x0a')
res.parse(b'Content-Type: text/plain\x0d\x0a\x0d\x0a')
res.parse(b'Hello World!')
print(res.code)
print(res.headers.content_type)
print(res.body)

# Or import the module, as in Google Python Style Guide

import Pyjo.Message.Response

# Parse
res = Pyjo.Message.Response.new()
res.parse(b'HTTP/1.0 200 OK\x0d\x0a')
res.parse(b'Content-Length: 12\x0d\x0a')
res.parse(b'Content-Type: text/plain\x0d\x0a\x0d\x0a')
res.parse(b'Hello World!')
print(res.code)
print(res.headers.content_type)
print(res.body)

# Build
res = Pyjo.Message.Response.new()
res.code = 200
res.headers.content_type = 'text/plain'
res.body = b'Hello World!'
print(res.to_bytes())

Pyjo.Message.Response is a container for HTTP responses, based on RFC 7230 and RFC 7231.

Events

Pyjo.Message.Response inherits all events from Pyjo.Message.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::Message::Response of Mojolicious 9.49.

class Pyjo.Message.Response.Pyjo_Message_Response(response: HTTPResponse | addinfourl | None = None, /, *, code: int | None = None, message: str | None = None, **kwargs: Any)[source]

Pyjo.Message.Response inherits all methods from Pyjo.Message and implements the following new ones.

__init__(response: HTTPResponse | addinfourl | None = None, /, *, code: int | None = None, message: str | None = None, **kwargs: Any) → None[source]
res = Pyjo.Message.Response.new()
res = Pyjo.Message.Response.new(code=200, message='OK')
res = Pyjo.Message.Response.new(urllib.request.urlopen('http://127.0.0.1/'))

Construct a new Pyjo.Message.Response object, from a response of urllib if it is given: an http.client.HTTPResponse, as urllib.request.urlopen() returns for HTTP, or a urllib.response.addinfourl, as it returns for other URLs and urllib.error.HTTPError is: its status, 200 for a response without one, as of a file: or data: URL, its headers, and the body without the transfer encoding, as http.client decodes it. It reads the response and closes it.

code
code = res.code
res.code = 200

HTTP response status code.

cookies() → list[Pyjo_Cookie_Response][source]
cookies(cookie: Pyjo_Cookie | Mapping[str, Any], /, *cookies: Anything) → Self
cookies = res.cookies()
res = res.cookies(Pyjo.Cookie.Response.new())
res = res.cookies({'name': 'foo', 'value': 'bar'})

Access response cookies, usually Pyjo.Cookie.Response objects.

# Names of all cookies
for cookie in res.cookies():
    print(cookie.name)
default_message(code: int | None = None) → str[source]
message = res.default_message()
message = res.default_message(418)

Generate default response message for status code, defaults to using code.

extract_start_line(buffer: bytearray) → bool | None[source]
boolean = res.extract_start_line(buffer)

Extract status-line from a bytearray, which loses it.

fix_headers() → Self[source]
res = res.fix_headers()

Make sure response has all required headers.

get_start_line_chunk(offset: int) → bytes[source]
data = res.get_start_line_chunk(offset)

Get a chunk of status-line data starting from a specific position. Note that this method finalizes the response.

property is_client_error: bool[source]
boolean = res.is_client_error

Check if this response has a 4xx status code.

property is_empty: bool[source]
boolean = res.is_empty

Check if this response has a 1xx, 204 or 304 status code.

property is_error: bool[source]
boolean = res.is_error

Check if this response has a 4xx or 5xx status code.

property is_info: bool[source]
boolean = res.is_info

Check if this response has a 1xx status code.

property is_redirect: bool[source]
boolean = res.is_redirect

Check if this response has a 3xx status code.

property is_server_error: bool[source]
boolean = res.is_server_error

Check if this response has a 5xx status code.

property is_success: bool[source]
boolean = res.is_success

Check if this response has a 2xx status code.

property max_message_size: int[source]
size = res.max_message_size
res.max_message_size = 1024

Maximum message size in bytes, defaults to the value of the PYJO_MAX_MESSAGE_SIZE environment variable or 2147483648 (2GiB). Setting the value to 0 will allow messages of indefinite size.

message
message = res.message
res.message = 'OK'

HTTP response status message.

property start_line_size: int[source]
size = res.start_line_size

Size of the status-line in bytes. Note that this method finalizes the response.

to_urllib(*, method: str | None = None, url: str | None = None) → HTTPResponse[source]
response = res.to_urllib()
response = res.to_urllib(method='HEAD', url='http://127.0.0.1/')

Turn response into an http.client.HTTPResponse, as urllib.request.urlopen() returns for HTTP, parsed by http.client from to_bytes(). The method of the request tells whether the response has a body, and the URL is what geturl() returns.

# A response for the code that uses urllib
response = Pyjo.Message.Response.new(code=200).set(body=b'Hi!').to_urllib()
print(response.read())