Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7195965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:41:08+00:00 2026-05-28T20:41:08+00:00

I’m building an Python application that has to connect and disconnect from Wifi on

  • 0

I’m building an Python application that has to connect and disconnect from Wifi on linux box. I’m using NetworkManager layer, through the nice networkmanager lib found in cnetworkmanager (a python CLI for NetworkManager http://vidner.net/martin/software/cnetworkmanager/ thanx to Martin Vidner), in a daemon (named stationd).
This daemon runs a gobject.MainLoop. Once a timeout_add_seconds awake (triggers by a action of user in GUI), I have to disconnect current running Wifi and connect to a new one:

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

from networkmanager import NetworkManager
import networkmanager.applet.settings as settings
from networkmanager.applet import USER_SERVICE 
from networkmanager.applet.service import NetworkManagerUserSettings, NetworkManagerSettings
import time
import memcache

import gobject
loop = gobject.MainLoop()

nm = NetworkManager()

dummy_handler = lambda *args: None

cache = memcache.Client(['127.0.0.1:11211',] )


def get_device(dev_spec, hint):
    candidates = []
    devs = NetworkManager().GetDevices()
    for dev in devs:
        if dev._settings_type() == hint:
            candidates.append(dev)
    if len(candidates) == 1:
        return candidates[0]
    for dev in devs:
        if dev["Interface"] == dev_spec:
            return dev
    print "Device '%s' not found" % dev_spec
    return None

def kill_allconnections():
    connections=nm['ActiveConnections']
    for c in connections:
        print c.object_path
        nm.DeactivateConnection(c.object_path)

class Wifi(object):

    def connect(self, ssid, security="open", password=None):
        "Connects to given Wifi network"
        c=None # connection settings
        us = NetworkManagerUserSettings([])
        if security=="open":
            c = settings.WiFi(ssid)
        elif security=="wep":
            c = settings.Wep(ssid, password)
        elif security=="wpa":
            c = settings.WpaPsk(ssid, password)
        else:
            raise AttributeError("invalid security model '%s'"%security)
        svc = USER_SERVICE
        svc_conn = us.addCon(c.conmap)
        hint = svc_conn.settings["connection"]["type"]
        dev = get_device("", hint)
        appath = "/"
        nm.ActivateConnection(svc, svc_conn, dev, appath, reply_handler=dummy_handler, error_handler=dummy_handler)


def change_network_settings():
    key="station:network:change"
    change=cache.get(key)
    if change is not None:
        print "DISCONNECT"
        kill_allconnections()
        print "CHANGE SETTINGS"
        wifi=cache.get(key+':wifi')
        if wifi is not None:
            ssid=cache.get(key+':wifi:ssid')
            security=cache.get(key+':wifi:security')
            password=cache.get(key+':wifi:password')
            print "SWITCHING TO %s"%ssid
            Wifi().connect(ssid, security, password)
        cache.delete(key)
    return True    

def mainloop():
   gobject.timeout_add_seconds(1, change_network_settings)
   try:
      loop.run()
   except KeyboardInterrupt:
      loop.quit()

if __name__=="__main__":
    mainloop()

This runs perfectly for a first connection (read : the box is not connected, daemon is ran and box connects flawlessly to Wifi). Issue is when I try to connect to another Wifi : kill_allconnections() is ran silently, and connect method raises an exception on nm.ActivateConnection:

Traceback (most recent call last):
  File "stationd.py", line 40, in change_network_settings
    Wifi().connect(ssid, security, password)
  File "/home/biopredictive/station/lib/network.py", line 88, in connect
    us = NetworkManagerUserSettings([])
  File "/home/biopredictive/station/lib/networkmanager/applet/service/__init__.py", line 71, in __init__
    super(NetworkManagerUserSettings, self).__init__(conmaps, USER_SERVICE)
  File "/home/biopredictive/station/lib/networkmanager/applet/service/__init__.py", line 33, in __init__
    dbus.service.Object.__init__(self, bus, opath, bus_name)
  File "/usr/lib/pymodules/python2.6/dbus/service.py", line 480, in __init__
    self.add_to_connection(conn, object_path)
  File "/usr/lib/pymodules/python2.6/dbus/service.py", line 571, in add_to_connection
    self._fallback)
KeyError: "Can't register the object-path handler for '/org/freedesktop/NetworkManagerSettings': there is already a handler"

It looks like my former connection didn’t release all its resources ?
I’m very new to gobject/dbus programming. Would you please help ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:41:08+00:00Added an answer on May 28, 2026 at 8:41 pm

    I have answered on the D-Bus mailing list. Quoting here for the archive:

    class Wifi(...):
        def connect(...):
            ...
            us = NetworkManagerUserSettings([]) 
    

    NetworkManagerUserSettings is a server, not a client. (It’s a design
    quirk of NM which was eliminated in NM 0.9)
    You should create only one for the daemon, not for each conection
    attempt.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning Python by building a simple PyGTK application that fetches data from
Building a python application that converts raw audio files into wave using sox on
I'm building a fairly large enterprise application made in python that on its first
I'm building a python application from some source code I've found Here I've managed
I'm building a centralized desktop application using Python/wxPython. One of the requirements is User
I am building an application using OpenCV that uses the webcam and runs some
I'm building a Mac OS X application that is to embed Python. My application
Hi I am building a simple application that will have a combo box to
I am building a python application to pull data from a website. The application
I'm building a multiuser realtime application with Google App Engine (Python) that would look

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.