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.Responseinherits all methods fromPyjo.Messageand 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.Responseobject, from a response of urllib if it is given: anhttp.client.HTTPResponse, asurllib.request.urlopen()returns for HTTP, or aurllib.response.addinfourl, as it returns for other URLs andurllib.error.HTTPErroris: its status,200for a response without one, as of afile:ordata:URL, its headers, and the body without the transfer encoding, ashttp.clientdecodes 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.Responseobjects.# 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.
- 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
4xxstatuscode.
- property is_empty: bool[source]¶
boolean = res.is_empty
Check if this response has a
1xx,204or304statuscode.
- property is_error: bool[source]¶
boolean = res.is_error
Check if this response has a
4xxor5xxstatuscode.
- property is_redirect: bool[source]¶
boolean = res.is_redirect
Check if this response has a
3xxstatuscode.
- property is_server_error: bool[source]¶
boolean = res.is_server_error
Check if this response has a
5xxstatuscode.
- property is_success: bool[source]¶
boolean = res.is_success
Check if this response has a
2xxstatuscode.
- 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_SIZEenvironment variable or2147483648(2GiB). Setting the value to0will 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, asurllib.request.urlopen()returns for HTTP, parsed byhttp.clientfromto_bytes(). The method of the request tells whether the response has a body, and the URL is whatgeturl()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())