Gio.NetworkMonitor

g GObject.GInterface GObject.GInterface Gio.NetworkMonitor Gio.NetworkMonitor GObject.GInterface->Gio.NetworkMonitor

Implementations:
 None

Methods

class get_default ()
  can_reach (connectable, cancellable)
  can_reach_async (connectable, cancellable, callback, *user_data)
  can_reach_finish (result)
  get_connectivity ()
  get_network_available ()
  get_network_metered ()

Virtual Methods

  do_can_reach (connectable, cancellable)
  do_can_reach_async (connectable, cancellable, callback, *user_data)
  do_can_reach_finish (result)
  do_network_changed (network_available)

Properties

Name Type Flags Short Description
connectivity Gio.NetworkConnectivity r Level of network connectivity
network-available bool r Whether the network is available
network-metered bool r Whether the network is metered

Signals

Name Short Description
network-changed Emitted when the network configuration changes.

Fields

None

Class Details

class Gio.NetworkMonitor
Bases:GObject.GInterface
Structure:Gio.NetworkMonitorInterface

Gio.NetworkMonitor 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.

New in version 2.32.

classmethod get_default()[source]
Returns:a Gio.NetworkMonitor
Return type:Gio.NetworkMonitor

Gets the default Gio.NetworkMonitor for the system.

New in version 2.32.

can_reach(connectable, cancellable)[source]
Parameters:
Raises:

GLib.Error

Returns:

True if connectable is reachable, False if not.

Return type:

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 when Gio.NetworkMonitor :network-available is False, if, for example, self can determine that connectable refers to a host on a local network.

If self believes that an attempt to connect to connectable will succeed, it will return True. Otherwise, it will return False and set error to an appropriate error (such as Gio.IOErrorEnum.HOST_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 use Gio.NetworkMonitor.can_reach_async().

New in version 2.32.

can_reach_async(connectable, cancellable, callback, *user_data)[source]
Parameters:

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 Gio.NetworkMonitor.can_reach().

When the operation is finished, callback will be called. You can then call Gio.NetworkMonitor.can_reach_finish() to get the result of the operation.

can_reach_finish(result)[source]
Parameters:result (Gio.AsyncResult) – a Gio.AsyncResult
Raises:GLib.Error
Returns:True if network is reachable, False if not.
Return type:bool

Finishes an async network connectivity test. See Gio.NetworkMonitor.can_reach_async().

get_connectivity()[source]
Returns:the network connectivity state
Return type:Gio.NetworkConnectivity

Gets a more detailed networking state than Gio.NetworkMonitor.get_network_available().

If Gio.NetworkMonitor :network-available is False, then the connectivity state will be Gio.NetworkConnectivity.LOCAL.

If Gio.NetworkMonitor :network-available is True, then the connectivity state will be Gio.NetworkConnectivity.FULL (if there is full Internet connectivity), Gio.NetworkConnectivity.LIMITED (if the host has a default route, but appears to be unable to actually reach the full Internet), or Gio.NetworkConnectivity.PORTAL (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 Gio.NetworkConnectivity.LIMITED and Gio.NetworkConnectivity.PORTAL, 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.

New in version 2.44.

get_network_available()[source]
Returns:whether the network is available
Return type: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 Gio.NetworkMonitor :network-available for more details.

New in version 2.32.

get_network_metered()[source]
Returns:whether the connection is metered
Return type:bool

Checks if the network is metered. See Gio.NetworkMonitor :network-metered for more details.

New in version 2.46.

do_can_reach(connectable, cancellable) virtual
Parameters:
Returns:

True if connectable is reachable, False if not.

Return type:

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 when Gio.NetworkMonitor :network-available is False, if, for example, monitor can determine that connectable refers to a host on a local network.

If monitor believes that an attempt to connect to connectable will succeed, it will return True. Otherwise, it will return False and set error to an appropriate error (such as Gio.IOErrorEnum.HOST_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 use Gio.NetworkMonitor.can_reach_async().

New in version 2.32.

do_can_reach_async(connectable, cancellable, callback, *user_data) virtual
Parameters:

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 Gio.NetworkMonitor.can_reach().

When the operation is finished, callback will be called. You can then call Gio.NetworkMonitor.can_reach_finish() to get the result of the operation.

do_can_reach_finish(result) virtual
Parameters:result (Gio.AsyncResult) – a Gio.AsyncResult
Returns:True if network is reachable, False if not.
Return type:bool

Finishes an async network connectivity test. See Gio.NetworkMonitor.can_reach_async().

do_network_changed(network_available) virtual
Parameters:network_available (bool) –

Signal Details

Gio.NetworkMonitor.signals.network_changed(network_monitor, network_available)
Signal Name:

network-changed

Flags:

RUN_LAST

Parameters:

Emitted when the network configuration changes.

New in version 2.32.

Property Details

Gio.NetworkMonitor.props.connectivity
Name:connectivity
Type:Gio.NetworkConnectivity
Default Value:Gio.NetworkConnectivity.FULL
Flags:READABLE

More detailed information about the host’s network connectivity. See Gio.NetworkMonitor.get_connectivity() and Gio.NetworkConnectivity for more details.

New in version 2.44.

Gio.NetworkMonitor.props.network_available
Name:network-available
Type:bool
Default Value:False
Flags:READABLE

Whether the network is considered available. That is, whether the system has a default route for at least one of IPv4 or IPv6.

Real-world networks are of course much more complicated than this; the machine may be connected to a wifi hotspot that requires payment before allowing traffic through, or may be connected to a functioning router that has lost its own upstream connectivity. Some hosts might only be accessible when a VPN is active. Other hosts might only be accessible when the VPN is not active. Thus, it is best to use Gio.NetworkMonitor.can_reach() or Gio.NetworkMonitor.can_reach_async() to test for reachability on a host-by-host basis. (On the other hand, when the property is False, the application can reasonably expect that no remote hosts at all are reachable, and should indicate this to the user in its UI.)

See also Gio.NetworkMonitor ::network-changed.

New in version 2.32.

Gio.NetworkMonitor.props.network_metered
Name:network-metered
Type:bool
Default Value:False
Flags:READABLE

Whether the network is considered metered. That is, whether the system has traffic flowing through the default connection that is subject to limitations set by service providers. For example, traffic might be billed by the amount of data transmitted, or there might be a quota on the amount of traffic per month. This is typical with tethered connections (3G and 4G) and in such situations, bandwidth intensive applications may wish to avoid network activity where possible if it will cost the user money or use up their limited quota.

If more information is required about specific devices then the system network management API should be used instead (for example, NetworkManager or ConnMan).

If this information is not available then no networks will be marked as metered.

See also Gio.NetworkMonitor :network-available.

New in version 2.46.