Re: Determine connectivity status



Mads Kiilerich wrote, On 03/10/2010 02:17 PM:
How can an application be made aware of the machines "online/offline" status? I assume there is a fine dbus method/event - but which?

Reply to self and archive: It is called State in NM lingo and can be accessed as below.

/Mads


#!/bin/env python
"""
Sample code for getting and following NetworkManager "state".
"""
# Based on:
# - http://projects.gnome.org/NetworkManager/developers/spec-08.html
# - test/nm-online.c (which however uses the depreciated state() method)
# - http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html

from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)

import dbus
bus = dbus.SystemBus()
proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")

NM_STATE_UNKNOWN = 0 # The NetworkManager daemon is in an unknown state.
NM_STATE_ASLEEP = 1 # The NetworkManager daemon is asleep and all interfaces managed by it are inactive.
NM_STATE_CONNECTING = 2 # The NetworkManager daemon is connecting a device.
NM_STATE_CONNECTED = 3 # The NetworkManager daemon is connected.
NM_STATE_DISCONNECTED = 4 # The NetworkManager daemon is disconnected.

state = proxy.Get("org.freedesktop.NetworkManager", "State",
    dbus_interface="org.freedesktop.DBus.Properties")
print 'Initial connected state:', state == NM_STATE_CONNECTED

def handle_StateChanged(state):
    print 'Changed connected state:', state == NM_STATE_CONNECTED
proxy.connect_to_signal('StateChanged', handle_StateChanged,
    dbus_interface='org.freedesktop.NetworkManager')

import gobject
loop = gobject.MainLoop()
loop.run()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]