NetworkMonitor
Added in version 2.32.
- class NetworkMonitor(*args, **kwargs)
GNetworkMonitor
provides an easy-to-use cross-platform API
for monitoring network connectivity. On Linux, the available
implementations are based on the kernel’s netlink interface and
on NetworkManager.
There is also an implementation for use inside Flatpak sandboxes.
Methods
- class NetworkMonitor
- can_reach(connectable: SocketConnectable, cancellable: Cancellable | None = None) bool
Attempts to determine whether or not the host pointed to by
connectable
can be reached, without actually trying to connect to it.This may return
True
even whenNetworkMonitor
:network-available isFalse
, if, for example,monitor
can determine thatconnectable
refers to a host on a local network.If
monitor
believes that an attempt to connect toconnectable
will succeed, it will returnTrue
. Otherwise, it will returnFalse
and seterror
to an appropriate error (such asHOST_UNREACHABLE
).Note that although this does not attempt to connect to
connectable
, it may still block for a brief period of time (eg, trying to do multicast DNS on the local network), so if you do not want to block, you should usecan_reach_async()
.Added in version 2.32.
- Parameters:
connectable – a
SocketConnectable
cancellable – a
Cancellable
, orNone
- can_reach_async(connectable: SocketConnectable, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None
Asynchronously attempts to determine whether or not the host pointed to by
connectable
can be reached, without actually trying to connect to it.For more details, see
can_reach()
.When the operation is finished,
callback
will be called. You can then callcan_reach_finish()
to get the result of the operation.- Parameters:
connectable – a
SocketConnectable
cancellable – a
Cancellable
, orNone
callback – a
AsyncReadyCallback
to call when the request is satisfieduser_data – the data to pass to callback function
- can_reach_finish(result: AsyncResult) bool
Finishes an async network connectivity test. See
can_reach_async()
.- Parameters:
result – a
AsyncResult
- get_connectivity() NetworkConnectivity
Gets a more detailed networking state than
get_network_available()
.If
NetworkMonitor
:network-available isFalse
, then the connectivity state will beLOCAL
.If
NetworkMonitor
:network-available isTrue
, then the connectivity state will beFULL
(if there is full Internet connectivity),LIMITED
(if the host has a default route, but appears to be unable to actually reach the full Internet), orPORTAL
(if the host is trapped behind a “captive portal” that requires some sort of login or acknowledgement before allowing full Internet access).Note that in the case of
LIMITED
andPORTAL
, it is possible that some sites are reachable but others are not. In this case, applications can attempt to connect to remote servers, but should gracefully fall back to their “offline” behavior if the connection attempt fails.Added in version 2.44.
- get_default() NetworkMonitor
Gets the default
NetworkMonitor
for the system.Added in version 2.32.
- get_network_available() bool
Checks if the network is available. “Available” here means that the system has a default route available for at least one of IPv4 or IPv6. It does not necessarily imply that the public Internet is reachable. See
NetworkMonitor
:network-available for more details.Added in version 2.32.
- get_network_metered() bool
Checks if the network is metered. See
NetworkMonitor
:network-metered for more details.Added in version 2.46.
Properties
Signals
- class NetworkMonitor.signals
- network_changed(network_available: bool) None
The type of the None singleton.
Added in version 2.32.
- Parameters:
network_available – the current value of
NetworkMonitor
:network-available
Virtual Methods
- class NetworkMonitor
- do_can_reach(connectable: SocketConnectable, cancellable: Cancellable | None = None) bool
Attempts to determine whether or not the host pointed to by
connectable
can be reached, without actually trying to connect to it.This may return
True
even whenNetworkMonitor
:network-available isFalse
, if, for example,monitor
can determine thatconnectable
refers to a host on a local network.If
monitor
believes that an attempt to connect toconnectable
will succeed, it will returnTrue
. Otherwise, it will returnFalse
and seterror
to an appropriate error (such asHOST_UNREACHABLE
).Note that although this does not attempt to connect to
connectable
, it may still block for a brief period of time (eg, trying to do multicast DNS on the local network), so if you do not want to block, you should usecan_reach_async()
.Added in version 2.32.
- Parameters:
connectable – a
SocketConnectable
cancellable – a
Cancellable
, orNone
- do_can_reach_async(connectable: SocketConnectable, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None
Asynchronously attempts to determine whether or not the host pointed to by
connectable
can be reached, without actually trying to connect to it.For more details, see
can_reach()
.When the operation is finished,
callback
will be called. You can then callcan_reach_finish()
to get the result of the operation.- Parameters:
connectable – a
SocketConnectable
cancellable – a
Cancellable
, orNone
callback – a
AsyncReadyCallback
to call when the request is satisfieduser_data – the data to pass to callback function
- do_can_reach_finish(result: AsyncResult) bool
Finishes an async network connectivity test. See
can_reach_async()
.- Parameters:
result – a
AsyncResult