6

I want to connect to a Wi-Fi network using Python on OS X (10.11). Based on CWInterface reference I figured out there is a iface.associateToNetwork_password_error_() method available, however when called it does not connect to the network nor it does cause any exception.

At the same time iface.disassociate() works correctly and disconnects WiFi.

This is the code I tried:

import objc

objc.loadBundle('CoreWLAN',
       bundle_path='/System/Library/Frameworks/CoreWLAN.framework',
       module_globals=globals())

iface = CWInterface.interface()

iface.associateToNetwork_password_error_(SSID, PASSWORD, None)

How can I connect to a specified network from Python on OS X and make sure the connection has been established?

techraf
  • 64,883
  • 27
  • 193
  • 198
  • What is `SSID`? It looks to me like that method takes a `CWNetwork` as its first parameter, but as far as I can tell you don't have an instance of one to give to it. – ArtOfWarfare Jan 23 '16 at 18:32

1 Answers1

6

I was able to successfully connect to my home network with the following:

import objc

objc.loadBundle('CoreWLAN',
                bundle_path = '/System/Library/Frameworks/CoreWLAN.framework',
                module_globals = globals())

iface = CWInterface.interface()

networks, error = iface.scanForNetworksWithName_error_('<Name Of Network>', None)

network = networks.anyObject()

success, error = iface.associateToNetwork_password_error_(network, '<Password Of Network>', None)

Two key things that I suspect you were missing:

  1. You weren't passing in a CWNetwork as your first parameter.
  2. You weren't checking return values (IE, success and error in the final call). They could have helped you with tracking down any other issues, perhaps.
ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
  • I tried this way but I'm getting an error that I don't understand the reason: ValueError: NSInvalidArgumentException - -[OC_PythonUnicode securityType]: unrecognized selector sent to instance 0x7fc861fdc040 – Tiago Beviláqua Sep 26 '18 at 12:17
  • @TiagoBeviláqua - I haven't use macOS in a few years. It may be that some APIs have substantially changed. I'd suggest posting a new question if you need help (I don't think anyone besides me will be notified about your comment.) – ArtOfWarfare Sep 26 '18 at 14:38