wifi.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import subprocess
4 import time
5 
7  proc = subprocess.Popen(['networksetup','-listallhardwareports'], stdout=subprocess.PIPE)
8  wifi = False
9  device_name = None
10  while True:
11  line = proc.stdout.readline()
12  if line == '':
13  break
14  words = line.rstrip().split()
15  if (2 < len(words) and
16  words[0] == 'Hardware' and words[1] == 'Port:' and words[2] == 'Wi-Fi'):
17  wifi = True
18  elif wifi and words[0] == 'Device:':
19  device_name = words[1]
20  else:
21  wifi = False
22  if device_name == None:
23  raise Exception('Wi-Fi device not found')
24  return device_name
25 
26 def get_status(device_name = get_device_name()):
27  proc = subprocess.Popen(['ifconfig', device_name], stdout=subprocess.PIPE)
28  wifi = False
29  status = None
30  while True:
31  line = proc.stdout.readline()
32  if line == '':
33  break
34  words = line.rstrip().split()
35  if words[0] == 'status:':
36  status = words[1]
37  if status == None:
38  raise Exception('Unknown Wi-Fi status')
39  return status
40 
41 def set_power(power, device_name = get_device_name()):
42  proc = subprocess.check_output(['networksetup', '-setairportpower', device_name, power])
43 
44 def get_ssid(device_name = get_device_name()):
45  proc = subprocess.Popen(['networksetup', '-getairportnetwork', device_name],
46  stdout=subprocess.PIPE)
47  ssid = None
48  while True:
49  line = proc.stdout.readline()
50  if line == '':
51  break
52  words = line.rstrip().split()
53  if (4 <= len(words) and
54  words[0] == 'Current' and words[1] == 'Wi-Fi' and words[2] == 'Network:'):
55  ssid = words[3]
56  if ssid == None:
57  raise Exception('Wi-Fi not connected')
58  return ssid
59 
60 def wait():
61  prev_status = get_status()
62  while True:
63  status = get_status()
64  if prev_status != status:
65  break
66 
67  if status != 'active':
68  set_power('off')
69  set_power('on')
70  time.sleep(2)
71 
72 
73 if __name__ == '__main__':
74  print('device name = %s' % device_name())
75  print('status = %s' % status())
76  while True:
77  wait()
78  if status() == 'active':
79  print('connected to %s' % ssid())
def set_power(power, device_name=get_device_name())
Definition: wifi.py:41
def get_status(device_name=get_device_name())
Definition: wifi.py:26
def wait()
Definition: wifi.py:60
def get_device_name()
Definition: wifi.py:6
def get_ssid(device_name=get_device_name())
Definition: wifi.py:44


tello_driver
Author(s): Jordy van Appeven
autogenerated on Wed May 13 2020 03:34:54