Pyjo.Transaction.HTTP - HTTP transaction

from Pyjo.Transaction.HTTP import HTTP

# Client
tx = HTTP()
tx.req.method = 'GET'
tx.req.url.parse('http://example.com')
tx.req.headers.accept = 'application/json'
print(tx.res.code)
print(tx.res.headers.content_type)
print(tx.res.body)
print(tx.remote_address)

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

import Pyjo.Transaction.HTTP

# Client
tx = Pyjo.Transaction.HTTP.new()
tx.req.method = 'GET'
tx.req.url.parse('http://example.com')
tx.req.headers.accept = 'application/json'
print(tx.res.code)
print(tx.res.headers.content_type)
print(tx.res.body)
print(tx.remote_address)

# Server
tx = Pyjo.Transaction.HTTP.new()
print(tx.req.method)
print(tx.req.url.to_abs())
print(tx.req.headers.accept)
print(tx.remote_address)
tx.res.code = 200
tx.res.headers.content_type = 'text/plain'
tx.res.body = b'Hello World!'

Pyjo.Transaction.HTTP is a container for HTTP transactions, based on RFC 7230 and RFC 7231.

The end of a with block resumes the transaction with resume(), so that a server writes the response that the block prepares. An exception that the block does not catch ends it with the default error: the response is replaced with a 500 Internal Server Error, which is resumed, and the exception goes to sys.stderr as a warning, so that the server writes the response.

# Server
with tx:
    tx.res.code = 200
    tx.res.headers.content_type = 'text/plain'
    tx.res.body = b'Hello World!'

Events

Pyjo.Transaction.HTTP inherits all events from Pyjo.Transaction and can emit the following new ones.

request

tx.on('request', lambda tx: ...)

Emitted when a request is ready and needs to be handled.

tx.on('request', lambda tx: tx.res.headers.header('X-Bender', 'Bite my shiny metal ass!'))

resume

tx.on('resume', lambda tx: ...)

Emitted when transaction is resumed.

unexpected

tx.on('unexpected', lambda tx, res: ...)

Emitted for unexpected 1xx responses that will be ignored.

tx.on('unexpected', lambda tx, res: tx.res.on('finish', lambda res: print('Follow-up response is finished.')))

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::Transaction::HTTP of Mojolicious 9.52.

class Pyjo.Transaction.HTTP.Pyjo_Transaction_HTTP(*, previous: Pyjo_Transaction_HTTP | None = None, **kwargs: Any)[source]

Pyjo.Transaction.HTTP inherits all methods from Pyjo.Transaction and implements the following new ones.

__init__(*, previous: Pyjo_Transaction_HTTP | None = None, **kwargs: Any) → None[source]
tx = Pyjo.Transaction.HTTP.new()
tx = Pyjo.Transaction.HTTP.new(req=Pyjo.Message.Request.new())

Construct a new Pyjo.Transaction.HTTP object.

client_read(chunk: bytes) → Self | None[source]
tx.client_read(data)

Read data client-side, used to implement user agents such as Pyjo.UserAgent.

client_write() → bytes[source]
data = tx.client_write()

Write data client-side, used to implement user agents such as Pyjo.UserAgent.

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

Check transaction for HEAD request and 1xx, 204 or 304 response.

property keep_alive: bool[source]
boolean = tx.keep_alive

Check if connection can be kept alive.

previous
previous = tx.previous
tx.previous = Pyjo.Transaction.HTTP.new()

Previous transaction that triggered this follow-up transaction, usually a Pyjo.Transaction.HTTP object.

# Paths of previous requests
print(tx.previous.previous.req.url.path)
print(tx.previous.req.url.path)
redirects() → list[Pyjo_Transaction_HTTP][source]
redirects = tx.redirects()

Return a list with all previous transactions that preceded this follow-up transaction.

# Paths of all previous requests
for previous in tx.redirects():
    print(previous.req.url.path)
resume() → Self[source]
tx = tx.resume()

Resume transaction.

server_read(chunk: bytes) → None[source]
tx.server_read(data)

Read data server-side, used to implement web servers such as Pyjo.Server.Daemon.

server_write() → bytes[source]
data = tx.server_write()

Write data server-side, used to implement web servers such as Pyjo.Server.Daemon.