-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (51 loc) · 2.51 KB
/
main.py
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
58
59
60
61
62
63
64
65
66
def web_config():
list_of_wifi = wifi_lookup()
if get_current_ip() == "0.0.0.0":
current_wifi, current_ip = "Cannot Connect to " + get_current_wifi(), ""
else:
current_wifi, current_ip = "Connected to " + get_current_wifi(), "IP: " + get_current_ip()
html = """
<html> <head> <title>CONFIG_ME - Home</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" href="data:,"> <style>html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}.button{display: inline-block; background-color: #e7bd3b; border: none; border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}.button2{background-color: #4286f4;}</style> </head> <body> <h1>Wifi Configuration</h1> <p><strong>"""+ current_wifi +"""</strong></p><p><strong>"""+ current_ip +"""</strong></p><form action="/"> <p> <select name="wifi_name"> <option value="" disabled selected>Please select the WIFI</option> """+ list_of_wifi +""" </select> </p><input type="submit" value="Connect" class="button"> </form> </body>
"""
return html
def get_current_ip():
current_ip = station.ifconfig()[0]
return current_ip
def get_current_wifi():
with open("config","r") as file:
config_ssid = file.read()
return config_ssid
def wifi_lookup():
global wifi_list
wifi_list, list_of_wifi, nets = [], """""", station.scan()
for net in nets:
wifi_name = net[0].decode('UTF-8')
wifi_list.append(wifi_name.replace(" ", "+") + " ")
list_of_wifi += "<option value=\"" + wifi_name + "\">" + net[0].decode('UTF-8') + "</option>"
return list_of_wifi
def connect_to_wifi(request):
wifi_params = request.find("/?wifi_name=")
if wifi_params == 6:#6 mean found
for wifi in wifi_list:
wifi_name = request.find(wifi)
if wifi_name == 18:#18 mean found
ssid = wifi.replace(" ","").replace("+", " ")
with open("config","w+") as file:
file.write(ssid)
print("reset")
reset.value(0)
else:
return web_config()
while True:
try:
prime_conn, prime_addr = prime_socket.accept()
print('Got a connection from %s' % str(prime_addr))
request = str(prime_conn.recv(1024))
response = connect_to_wifi(request)
prime_conn.send('HTTP/1.1 200 OK\n')
prime_conn.send('Content-Type: text/html\n')
prime_conn.send('Connection: close\n\n')
prime_conn.sendall(response)
prime_conn.close()
except Exception as e:
pass#continue to the next loop