Gio.SocketClient¶
class — extends GObject.Object
GSocketClient is a lightweight high-level utility class for connecting to
a network host using a connection oriented socket type.
You create a GSocketClient object, set any options you want, and then
call a sync or async connect operation, which returns a
SocketConnection subclass on success.
The type of the SocketConnection object returned depends on the
type of the underlying socket that is in use. For instance, for a TCP/IP
connection it will be a TcpConnection.
As GSocketClient is a lightweight object, you don't need to cache it. You
can just create a new one any time you need one.
Constructors¶
new¶
Creates a new SocketClient with the default options.
Methods¶
add_application_proxy¶
Enable proxy protocols to be handled by the application. When the
indicated proxy protocol is returned by the ProxyResolver,
SocketClient will consider this protocol as supported but will
not try to find a Proxy instance to handle handshaking. The
application must check for this case by calling
SocketConnection.get_remote_address on the returned
SocketConnection, and seeing if it's a ProxyAddress of the
appropriate type, to determine whether or not it needs to handle
the proxy handshaking itself.
This should be used for proxy protocols that are dialects of another protocol such as HTTP proxy. It also allows cohabitation of proxy protocols that are reused between protocols. A good example is HTTP. It can be used to proxy HTTP, FTP and Gopher and can also be use as generic socket proxy through the HTTP CONNECT method.
When the proxy is detected as being an application proxy, TLS handshake will be skipped. This is required to let the application do the proxy specific handshake.
Parameters:
protocol— The proxy protocol
connect¶
def connect(self, connectable: SocketConnectable, cancellable: Cancellable | None = ...) -> SocketConnection
Tries to resolve the connectable and make a network connection to it.
Upon a successful connection, a new SocketConnection is constructed
and returned. The caller owns this new object and must drop their
reference to it when finished with it.
The type of the SocketConnection object returned depends on the type of
the underlying socket that is used. For instance, for a TCP/IP connection
it will be a TcpConnection.
The socket created will be the same family as the address that the
connectable resolves to, unless family is set with SocketClient.set_family
or indirectly via SocketClient.set_local_address. The socket type
defaults to SocketType.STREAM but can be set with
SocketClient.set_socket_type.
If a local address is specified with SocketClient.set_local_address the
socket will be bound to this address before connecting.
Parameters:
connectable— aSocketConnectablespecifying the remote address.cancellable— optionalCancellableobject,Noneto ignore.
connect_async¶
def connect_async(self, connectable: SocketConnectable, cancellable: Cancellable | None = ..., callback: Callable[[SocketClient | None, AsyncResult], None] | None = ...) -> None
This is the asynchronous version of SocketClient.connect.
You may wish to prefer the asynchronous version even in synchronous command line programs because, since 2.60, it implements RFC 8305 "Happy Eyeballs" recommendations to work around long connection timeouts in networks where IPv6 is broken by performing an IPv4 connection simultaneously without waiting for IPv6 to time out, which is not supported by the synchronous call. (This is not an API guarantee, and may change in the future.)
When the operation is finished callback will be
called. You can then call SocketClient.connect_finish to get
the result of the operation.
Parameters:
connectable— aSocketConnectablespecifying the remote address.cancellable— aCancellable, orNonecallback— aGAsyncReadyCallback
connect_finish¶
Finishes an async connect operation. See SocketClient.connect_async
Parameters:
result— aAsyncResult.
connect_to_host¶
def connect_to_host(self, host_and_port: str, default_port: int, cancellable: Cancellable | None = ...) -> SocketConnection
This is a helper function for SocketClient.connect.
Attempts to create a TCP connection to the named host.
host_and_port may be in any of a number of recognized formats; an IPv6
address, an IPv4 address, or a domain name (in which case a DNS
lookup is performed). Quoting with [] is supported for all address
types. A port override may be specified in the usual way with a
colon. Ports may be given as decimal numbers or symbolic names (in
which case an /etc/services lookup is performed).
If no port override is given in host_and_port then default_port will be
used as the port number to connect to.
In general, host_and_port is expected to be provided by the user (allowing
them to give the hostname, and a port override if necessary) and
default_port is expected to be provided by the application.
In the case that an IP address is given, a single connection attempt is made. In the case that a name is given, multiple connection attempts may be made, in turn and according to the number of address records in DNS, until a connection succeeds.
Upon a successful connection, a new SocketConnection is constructed
and returned. The caller owns this new object and must drop their
reference to it when finished with it.
In the event of any failure (DNS error, service not found, no hosts
connectable) None is returned and error (if non-None) is set
accordingly.
Parameters:
host_and_port— the name and optionally port of the host to connect todefault_port— the default port to connect tocancellable— aCancellable, orNone
connect_to_host_async¶
def connect_to_host_async(self, host_and_port: str, default_port: int, cancellable: Cancellable | None = ..., callback: Callable[[SocketClient | None, AsyncResult], None] | None = ...) -> None
This is the asynchronous version of SocketClient.connect_to_host.
When the operation is finished callback will be
called. You can then call SocketClient.connect_to_host_finish to get
the result of the operation.
Parameters:
host_and_port— the name and optionally the port of the host to connect todefault_port— the default port to connect tocancellable— aCancellable, orNonecallback— aGAsyncReadyCallback
connect_to_host_finish¶
Finishes an async connect operation. See SocketClient.connect_to_host_async
Parameters:
result— aAsyncResult.
connect_to_service¶
def connect_to_service(self, domain: str, service: str, cancellable: Cancellable | None = ...) -> SocketConnection
Attempts to create a TCP connection to a service.
This call looks up the SRV record for service at domain for the
"tcp" protocol. It then attempts to connect, in turn, to each of
the hosts providing the service until either a connection succeeds
or there are no hosts remaining.
Upon a successful connection, a new SocketConnection is constructed
and returned. The caller owns this new object and must drop their
reference to it when finished with it.
In the event of any failure (DNS error, service not found, no hosts
connectable) None is returned and error (if non-None) is set
accordingly.
Parameters:
domain— a domain nameservice— the name of the service to connect tocancellable— aCancellable, orNone
connect_to_service_async¶
def connect_to_service_async(self, domain: str, service: str, cancellable: Cancellable | None = ..., callback: Callable[[SocketClient | None, AsyncResult], None] | None = ...) -> None
This is the asynchronous version of
SocketClient.connect_to_service.
Parameters:
domain— a domain nameservice— the name of the service to connect tocancellable— aCancellable, orNonecallback— aGAsyncReadyCallback
connect_to_service_finish¶
Finishes an async connect operation. See SocketClient.connect_to_service_async
Parameters:
result— aAsyncResult.
connect_to_uri¶
def connect_to_uri(self, uri: str, default_port: int, cancellable: Cancellable | None = ...) -> SocketConnection
This is a helper function for SocketClient.connect.
Attempts to create a TCP connection with a network URI.
uri may be any valid URI containing an "authority" (hostname/port)
component. If a port is not specified in the URI, default_port
will be used. TLS will be negotiated if SocketClient:tls is True.
(SocketClient does not know to automatically assume TLS for
certain URI schemes.)
Using this rather than SocketClient.connect or
SocketClient.connect_to_host allows SocketClient to
determine when to use application-specific proxy protocols.
Upon a successful connection, a new SocketConnection is constructed
and returned. The caller owns this new object and must drop their
reference to it when finished with it.
In the event of any failure (DNS error, service not found, no hosts
connectable) None is returned and error (if non-None) is set
accordingly.
Parameters:
uri— A network URIdefault_port— the default port to connect tocancellable— aCancellable, orNone
connect_to_uri_async¶
def connect_to_uri_async(self, uri: str, default_port: int, cancellable: Cancellable | None = ..., callback: Callable[[SocketClient | None, AsyncResult], None] | None = ...) -> None
This is the asynchronous version of SocketClient.connect_to_uri.
When the operation is finished callback will be
called. You can then call SocketClient.connect_to_uri_finish to get
the result of the operation.
Parameters:
uri— a network uridefault_port— the default port to connect tocancellable— aCancellable, orNonecallback— aGAsyncReadyCallback
connect_to_uri_finish¶
Finishes an async connect operation. See SocketClient.connect_to_uri_async
Parameters:
result— aAsyncResult.
get_enable_proxy¶
Gets the proxy enable state; see SocketClient.set_enable_proxy
get_family¶
Gets the socket family of the socket client.
See SocketClient.set_family for details.
get_local_address¶
Gets the local address of the socket client.
See SocketClient.set_local_address for details.
get_protocol¶
Gets the protocol name type of the socket client.
See SocketClient.set_protocol for details.
get_proxy_resolver¶
Gets the ProxyResolver being used by client. Normally, this will
be the resolver returned by ProxyResolver.get_default, but you
can override it with SocketClient.set_proxy_resolver.
get_socket_type¶
Gets the socket type of the socket client.
See SocketClient.set_socket_type for details.
get_timeout¶
Gets the I/O timeout time for sockets created by client.
See SocketClient.set_timeout for details.
get_tls¶
Gets whether client creates TLS connections. See
SocketClient.set_tls for details.
get_tls_validation_flags¶
:::warning Deprecated since 2.72 This API is deprecated. :::
Gets the TLS validation flags used creating TLS connections via
client.
This function does not work as originally designed and is impossible
to use correctly. See SocketClient:tls-validation-flags for more
information.
set_enable_proxy¶
Sets whether or not client attempts to make connections via a
proxy server. When enabled (the default), SocketClient will use a
ProxyResolver to determine if a proxy protocol such as SOCKS is
needed, and automatically do the necessary proxy negotiation.
See also SocketClient.set_proxy_resolver.
Parameters:
enable— whether to enable proxies
set_family¶
Sets the socket family of the socket client.
If this is set to something other than SocketFamily.INVALID
then the sockets created by this object will be of the specified
family.
This might be useful for instance if you want to force the local connection to be an ipv4 socket, even though the address might be an ipv6 mapped to ipv4 address.
Parameters:
family— aSocketFamily
set_local_address¶
Sets the local address of the socket client.
The sockets created by this object will bound to the
specified address (if not None) before connecting.
This is useful if you want to ensure that the local side of the connection is on a specific port, or on a specific interface.
Parameters:
address— aSocketAddress, orNone
set_protocol¶
Sets the protocol of the socket client. The sockets created by this object will use of the specified protocol.
If protocol is SocketProtocol.DEFAULT that means to use the default
protocol for the socket family and type.
Parameters:
protocol— aSocketProtocol
set_proxy_resolver¶
Overrides the ProxyResolver used by client. You can call this if
you want to use specific proxies, rather than using the system
default proxy settings.
Note that whether or not the proxy resolver is actually used
depends on the setting of SocketClient:enable-proxy, which is not
changed by this function (but which is True by default)
Parameters:
proxy_resolver— aProxyResolver, orNonefor the default.
set_socket_type¶
Sets the socket type of the socket client. The sockets created by this object will be of the specified type.
It doesn't make sense to specify a type of SocketType.DATAGRAM,
as GSocketClient is used for connection oriented services.
Parameters:
type— aSocketType
set_timeout¶
Sets the I/O timeout for sockets created by client. timeout is a
time in seconds, or 0 for no timeout (the default).
The timeout value affects the initial connection attempt as well,
so setting this may cause calls to SocketClient.connect, etc,
to fail with IOErrorEnum.TIMED_OUT.
Parameters:
timeout— the timeout
set_tls¶
Sets whether client creates TLS (aka SSL) connections. If tls is
True, client will wrap its connections in a TlsClientConnection
and perform a TLS handshake when connecting.
Note that since SocketClient must return a SocketConnection,
but TlsClientConnection is not a SocketConnection, this
actually wraps the resulting TlsClientConnection in a
TcpWrapperConnection when returning it. You can use
TcpWrapperConnection.get_base_io_stream on the return value
to extract the TlsClientConnection.
If you need to modify the behavior of the TLS handshake (eg, by
setting a client-side certificate to use, or connecting to the
TlsConnection::accept-certificate signal), you can connect to
client's SocketClient::event signal and wait for it to be
emitted with SocketClientEvent.TLS_HANDSHAKING, which will give you
a chance to see the TlsClientConnection before the handshake
starts.
Parameters:
tls— whether to use TLS
set_tls_validation_flags¶
:::warning Deprecated since 2.72 This API is deprecated. :::
Sets the TLS validation flags used when creating TLS connections
via client. The default value is TlsCertificateFlags.VALIDATE_ALL.
This function does not work as originally designed and is impossible
to use correctly. See SocketClient:tls-validation-flags for more
information.
Parameters:
flags— the validation flags
Virtual methods¶
do_event¶
def do_event(self, event: SocketClientEvent | int, connectable: SocketConnectable, connection: IOStream) -> None
Properties¶
enable_proxy¶
Enable proxy support.
family¶
The address family to use for socket construction.
local_address¶
The local address constructed sockets will be bound to.
protocol¶
The protocol to use for socket construction, or 0 for default.
proxy_resolver¶
The proxy resolver to use
timeout¶
The I/O timeout for sockets, in seconds, or 0 for none.
tls¶
Whether to create TLS connections.
tls_validation_flags¶
:::warning Deprecated since 2.72 This API is deprecated. :::
The TLS validation flags used when creating TLS connections. The
default value is TlsCertificateFlags.VALIDATE_ALL.
GLib guarantees that if certificate verification fails, at least one
flag will be set, but it does not guarantee that all possible flags
will be set. Accordingly, you may not safely decide to ignore any
particular type of error. For example, it would be incorrect to mask
TlsCertificateFlags.EXPIRED if you want to allow expired certificates,
because this could potentially be the only error flag set even if
other problems exist with the certificate. Therefore, there is no
safe way to use this property. This is not a horrible problem,
though, because you should not be attempting to ignore validation
errors anyway. If you really must ignore TLS certificate errors,
connect to the SocketClient::event signal, wait for it to be
emitted with SocketClientEvent.TLS_HANDSHAKING, and use that to
connect to TlsConnection::accept-certificate.
type¶
The type to use for socket construction.
Signals¶
event¶
def on_event(self, event: SocketClientEvent, connectable: SocketConnectable, connection: IOStream | None) -> None: ...
Emitted when client's activity on connectable changes state.
Among other things, this can be used to provide progress
information about a network connection in the UI. The meanings of
the different event values are as follows:
-
SocketClientEvent.RESOLVING:clientis about to look upconnectablein DNS.connectionwill beNone. -
SocketClientEvent.RESOLVED:clienthas successfully resolvedconnectablein DNS.connectionwill beNone. -
SocketClientEvent.CONNECTING:clientis about to make a connection to a remote host; either a proxy server or the destination server itself.connectionis theSocketConnection, which is not yet connected. Since GLib 2.40, you can access the remote address viaSocketConnection.get_remote_address. -
SocketClientEvent.CONNECTED:clienthas successfully connected to a remote host.connectionis the connectedSocketConnection. -
SocketClientEvent.PROXY_NEGOTIATING:clientis about to negotiate with a proxy to get it to connect toconnectable.connectionis theSocketConnectionto the proxy server. -
SocketClientEvent.PROXY_NEGOTIATED:clienthas negotiated a connection toconnectablethrough a proxy server.connectionis the stream returned fromProxy.connect, which may or may not be aSocketConnection. -
SocketClientEvent.TLS_HANDSHAKING:clientis about to begin a TLS handshake.connectionis aTlsClientConnection. -
SocketClientEvent.TLS_HANDSHAKED:clienthas successfully completed the TLS handshake.connectionis aTlsClientConnection. -
SocketClientEvent.COMPLETE:clienthas either successfully connected toconnectable(in which caseconnectionis theSocketConnectionthat it will be returning to the caller) or has failed (in which caseconnectionisNoneand the client is about to return an error).
Each event except SocketClientEvent.COMPLETE may be emitted
multiple times (or not at all) for a given connectable (in
particular, if client ends up attempting to connect to more than
one address). However, if client emits the SocketClient::event
signal at all for a given connectable, then it will always emit
it with SocketClientEvent.COMPLETE when it is done.
Note that there may be additional SocketClientEvent values in
the future; unrecognized event values should be ignored.