Pyjo.IOLoop.Client - Non-blocking TCP/IP and UNIX domain socket client

from Pyjo.IOLoop.Client import Client

# Create socket connection
client = Client()
client.on('connect', lambda client, handle: ...)
client.on('error', lambda client, err: ...)
client.connect(address='example.com', port=80)

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

import Pyjo.IOLoop.Client

# Create socket connection
client = Pyjo.IOLoop.Client.new()
client.on('connect', lambda client, handle: ...)
client.on('error', lambda client, err: ...)
client.connect(address='example.com', port=80)

# Start reactor if necessary
if not client.reactor.is_running:
    client.reactor.start()

Pyjo.IOLoop.Client opens TCP/IP and UNIX domain socket connections for Pyjo.IOLoop.

The end of a with block closes the connection.

Events

Pyjo.IOLoop.Client inherits all events from Pyjo.EventEmitter and can emit the following new ones.

connect

client.on('connect', lambda client, handle: ...)

Emitted once the connection is established, with its socket.socket, or the ssl.SSLSocket once TLS has been negotiated.

error

client.on('error', lambda client, err: ...)

Emitted if an error occurs on the connection, fatal if unhandled. The error is a message, or the OSError of the system.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::IOLoop::Client of Mojolicious 9.49.

class Pyjo.IOLoop.Client.Pyjo_IOLoop_Client(*, reactor: Pyjo_Reactor | None = None)[source]

Pyjo.IOLoop.Client inherits all methods from Pyjo.EventEmitter and implements the following new ones.

classmethod can_nnr() → bool[source]
boolean = Pyjo.IOLoop.Client.object.can_nnr()

True if non-blocking name resolution support is enabled, as it is unless the PYJO_NO_NNR environment variable is set. The names are resolved in a thread.

classmethod can_socks() → bool[source]
boolean = Pyjo.IOLoop.Client.object.can_socks()

True if SOCKS5 support is enabled, as it is unless the PYJO_NO_SOCKS environment variable is set.

close() → None[source]
client.close()

Stop connecting and close a socket that is not connected yet, as it happens when the client is freed.

connect(**options: Any) → None[source]
client.connect(address='127.0.0.1', port=3000)

Open a socket connection to a remote host.

These options are currently available:

address
address='mojolicious.org'

Address or host name of the peer to connect to, defaults to 127.0.0.1.

handle
handle=handle

Use an already prepared socket.socket object.

path
path='/tmp/myapp.sock'

Path of UNIX domain socket to connect to.

port
port=80

Port to connect to, defaults to 80 or 443 with tls option.

source_address
source_address=('127.0.0.1', 0)

Local address and port to connect from.

socks_address
socks_address='127.0.0.1'

Address or host name of SOCKS5 proxy server to use for connection.

socks_pass
socks_pass='secr3t'

Password to use for SOCKS5 authentication.

socks_port
socks_port=9050

Port of SOCKS5 proxy server to use for connection.

socks_user
socks_user='sri'

Username to use for SOCKS5 authentication.

timeout
timeout=15

Maximum amount of time in seconds establishing connection may take before getting canceled, defaults to 10.

tls
tls=True

Enable TLS.

tls_ca
tls_ca='/etc/tls/ca.crt'

Path to TLS certificate authority file.

tls_cert
tls_cert='/etc/tls/client.crt'

Path to the TLS certificate file.

tls_context
tls_context=ssl.create_default_context()

The ssl.SSLContext to use instead of the one that the other options make.

tls_hostname
tls_hostname='mojolicious.org'

Name of the server for SNI and the verification of its certificate instead of address, an empty one for no SNI.

tls_key
tls_key='/etc/tls/client.key'

Path to the TLS key file.

property reactor: Pyjo_Reactor[source]
reactor = client.reactor
client.reactor = Pyjo.Reactor.Poll.new()

Low-level event reactor, defaults to the reactor attribute value of the global Pyjo.IOLoop singleton. Note that this attribute is weakened.