-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwlan.py
More file actions
57 lines (46 loc) · 1.84 KB
/
wlan.py
File metadata and controls
57 lines (46 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import network
import utime
from ubinascii import hexlify
class WLANException(Exception):
pass
class WLAN:
def __init__(self, ssid, password):
self.__ssid = ssid
self.__pass = password
self.__access_point = network.WLAN(network.AP_IF)
self.__station = network.WLAN(network.STA_IF)
self.__station.active(True)
self.__hostname = self.__station.config('dhcp_hostname')
@staticmethod
def mac_address():
return hexlify(network.WLAN().config('mac'), ':').decode('utf-8')
def set_hostname(self, prefix, mac_length=-4):
mac_address = WLAN.mac_address().replace(':', '')
if abs(mac_length) > len(mac_address):
print('[-] WLAN.set_hostname: mac_length({0}) is longer '
'than mac_address({2}), using full lenght'.format(mac_length, mac_address))
mac_length = len(mac_address)
if mac_length < 0:
hostname = prefix + mac_address[mac_length:]
else:
hostname = prefix + mac_address[:mac_length]
self.__station.config(dhcp_hostname=hostname)
self.__hostname = hostname
return hostname
def get_hostname(self):
return self.__hostname
def connect(self, delay=10000):
self.__access_point.active(False)
self.__station.active(True)
cstart_ms = utime.ticks_ms()
if not self.__station.isconnected():
self.__station.connect(self.__ssid, self.__pass)
while not self.__station.isconnected():
if utime.ticks_diff(cstart_ms, utime.ticks_ms()) > delay:
raise WLANException('[-] WLAN.connect() timed out!')
utime.sleep_ms(100)
return True
def isconnected(self):
return self.__station.isconnected()
def ip(self):
return self.__station.ifconfig()[0]