Pyjo.Transaction - Transaction base class¶
from Pyjo.Transaction import Transaction
class MyTransaction(Transaction):
def client_read(self, chunk): ...
def client_write(self): ...
def server_read(self, chunk): ...
def server_write(self): ...
# Or import the module, as in Google Python Style Guide
import Pyjo.Transaction
class MyTransaction(Pyjo.Transaction.object):
def client_read(self, chunk): ...
def client_write(self): ...
def server_read(self, chunk): ...
def server_write(self): ...
Pyjo.Transaction is an abstract base class for transactions, like
Pyjo.Transaction.HTTP and Pyjo.Transaction.WebSocket.
Events¶
Pyjo.Transaction inherits all events from Pyjo.EventEmitter and
can emit the following new ones.
connection¶
tx.on('connection', lambda tx, connection: ...)
Emitted when a connection has been assigned to transaction.
finish¶
tx.on('finish', lambda tx: ...)
Emitted when transaction is finished.
See also¶
Pyjoyment, https://mojolicious.org.
Ported from Mojo::Transaction of Mojolicious 9.49.
- class Pyjo.Transaction.Pyjo_Transaction(*, kept_alive: bool | None = None, local_address: str | None = None, local_port: int | None = None, original_remote_address: str | None = None, remote_port: int | None = None, req: Pyjo_Message_Request | None = None, res: Pyjo_Message_Response | None = None)[source]¶
Pyjo.Transactioninherits all methods fromPyjo.EventEmitterand implements the following new ones.- __init__(*, kept_alive: bool | None = None, local_address: str | None = None, local_port: int | None = None, original_remote_address: str | None = None, remote_port: int | None = None, req: Pyjo_Message_Request | None = None, res: Pyjo_Message_Response | None = None) None[source]¶
tx = Pyjo.Transaction.HTTP.new() tx = Pyjo.Transaction.HTTP.new(req=Pyjo.Message.Request.new())
Construct a new transaction object.
- client_read(chunk: bytes) Anything[source]¶
tx.client_read(data)
Read data client-side, used to implement user agents such as
Pyjo.UserAgent. Meant to be overloaded in a subclass.
- client_write() bytes[source]¶
data = tx.client_write()
Write data client-side, used to implement user agents such as
Pyjo.UserAgent. Meant to be overloaded in a subclass.
- closed() Self[source]¶
tx = tx.closed()
Same as
completed(), but also indicates that all transaction data has been sent.
- property connection: str | None[source]¶
id = tx.connection tx.connection = id
Connection identifier, setting it emits the
connectionevent.
- property error: Error | None[source]¶
err = tx.error
Get request or response error and return
Noneif there is no error.# Longer version err = tx.req.error or tx.res.error # Check for 4xx/5xx response and connection errors if err := tx.error: if 'code' in err: raise Exception(f'{err["code"]} response: {err["message"]}') raise Exception(f'Connection error: {err["message"]}')
- property is_websocket: bool[source]¶
boolean = tx.is_websocket
False, this is not a
Pyjo.Transaction.WebSocketobject.
- property kept_alive: bool | None[source]¶
boolean = tx.kept_alive tx.kept_alive = boolean
Connection has been kept alive.
- property local_address: str | None[source]¶
address = tx.local_address tx.local_address = '127.0.0.1'
Local interface address.
- property local_port: int | None[source]¶
port = tx.local_port tx.local_port = 8080
Local interface port.
- original_remote_address¶
address = tx.original_remote_address tx.original_remote_address = '127.0.0.1'
Remote interface address.
- property remote_address: str | None[source]¶
address = tx.remote_address tx.remote_address = '127.0.0.1'
Same as
original_remote_addressunlessreqhas been performed via aPyjo.Message.Request.Pyjo_Message_Request.reverse_proxy. If so then the last value ofX-Forwarded-Forheader is returned. Additionally ifPyjo.Message.Request.Pyjo_Message_Request.trusted_proxiesare also provided then the original address must be trusted and anyX-Forwarded-Forentries that are trusted are ignored, returning the last untrusted address or the first address if all are trusted.
- property remote_port: int | None[source]¶
port = tx.remote_port tx.remote_port = 8081
Remote interface port.
- property req: Pyjo_Message_Request[source]¶
req = tx.req tx.req = Pyjo.Message.Request.new()
HTTP request, defaults to a
Pyjo.Message.Requestobject.# Access request information method = tx.req.method url = tx.req.url.to_abs() info = tx.req.url.to_abs().userinfo host = tx.req.url.to_abs().host agent = tx.req.headers.user_agent custom = tx.req.headers.header('Custom-Header') data = tx.req.body string = tx.req.text params = tx.req.params.to_dict() all = tx.req.uploads() value = tx.req.json() foo = tx.req.json('/23/foo') dom = tx.req.dom() bar = tx.req.dom('div.bar').first().text
- property res: Pyjo_Message_Response[source]¶
res = tx.res tx.res = Pyjo.Message.Response.new()
HTTP response, defaults to a
Pyjo.Message.Responseobject.# Access response information code = tx.res.code message = tx.res.message server = tx.res.headers.server custom = tx.res.headers.header('Custom-Header') data = tx.res.body string = tx.res.text value = tx.res.json() foo = tx.res.json('/23/foo') dom = tx.res.dom() bar = tx.res.dom('div.bar').first().text
- property result: Pyjo_Message_Response[source]¶
res = tx.result
Returns the
Pyjo.Message.Responseobject fromresor raises an exception if a connection error has occurred.# Fine grained response handling (raises on connection errors) res = tx.result if res.is_success: print(res.body) elif res.is_error: print(res.message) elif res.code == 301: print(res.headers.location) else: print('Whatever...')
- server_read(chunk: bytes) Anything[source]¶
tx.server_read(data)
Read data server-side, used to implement web servers such as
Pyjo.Server.Daemon. Meant to be overloaded in a subclass.
- server_write() bytes[source]¶
data = tx.server_write()
Write data server-side, used to implement web servers such as
Pyjo.Server.Daemon. Meant to be overloaded in a subclass.