-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.py
97 lines (85 loc) · 1.96 KB
/
boot.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
v = """
import time
import network
import socket
import ubinascii
import json
def parseFetch(val):
remaining = val[0:8]
payload = val[8:]
print(val)
return (remaining,payload)
def fetch(name,s,saveName=None):
s.send(name)
remaining,ret = parseFetch(s.recv(128))
s.send("12345678")
remaining = int(remaining)
total = 0
if saveName:
script_file = open(saveName,"w")
else:
script_file = open(name,"w")
script_file.write(ret)
print("recieved {0} chars".format(len(ret)))
while remaining > 120:
try:
remaining,payload = parseFetch(s.recv(128))
s.send("12345678")
remaining = int(remaining)
script_file.write(payload)
total += len(payload)
print("recieved {0} chars with {1} left to go.".format(len(payload),remaining))
except OSError as OSE:
print(ret)
print("Nothing left to get. Moving on.")
break
print("Got all of {0} it had a total of {1} bytes".format(name,total))
script_file.close()
return total
def boot():
wlan = network.WLAN()
failsafe = 0
while wlan.status() != network.STAT_GOT_IP:
failsafe += 1
if failsafe > 500:
break
time.sleep_ms(100)
print("Nope!",wlan.status(),failsafe)
print("Connected! ",wlan.ifconfig()[0])
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.connect(("192.168.0.17",41990))
except:
print("Couldn't find host!")
return
mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
s.send(mac)
config = json.loads(s.recv(128))
config["MAC"] = mac
s.settimeout(5)
print(config)
print(config["Main"])
print("Getting script")
main_str = fetch(config["Main"],s,"main.py")
print("Received main.")
print("There are {0} files to get next.".format(len(config["Files"])))
for script in config["Files"]:
script_string = fetch(script,s)
s.send("Complete")
s.close()
boot()
del time
del network
del socket
del ubinascii
del json
del boot
del fetch
del parseFetch
import gc
gc.collect()
print("Main has been writen.")
"""
f = open("boot.py","w")
f.write(v)
f.close()