|
| 1 | +import os |
| 2 | +import usocket |
| 3 | +import network,time |
| 4 | +import network , ubinascii |
| 5 | + |
| 6 | +def do_connect(): |
| 7 | + global connected |
| 8 | + sta_if = network.WLAN(network.STA_IF) |
| 9 | + sta_if.active(True) |
| 10 | + print('connecting to network...') |
| 11 | + sta_if.connect('webduino.io', 'webduino') |
| 12 | + cnt = 0 |
| 13 | + while not sta_if.isconnected(): |
| 14 | + cnt = cnt + 1 |
| 15 | + time.sleep(0.5) |
| 16 | + if cnt == 60: |
| 17 | + break |
| 18 | + connected = sta_if.isconnected() |
| 19 | + print('network config:', sta_if.ifconfig()) |
| 20 | + |
| 21 | +class Response: |
| 22 | + |
| 23 | + def __init__(self, f): |
| 24 | + self.raw = f |
| 25 | + self.encoding = "utf-8" |
| 26 | + self._cached = None |
| 27 | + |
| 28 | + def close(self): |
| 29 | + if self.raw: |
| 30 | + self.raw.close() |
| 31 | + self.raw = None |
| 32 | + self._cached = None |
| 33 | + |
| 34 | + @property |
| 35 | + def content(self): |
| 36 | + if self._cached is None: |
| 37 | + try: |
| 38 | + self._cached = self.raw.read() |
| 39 | + finally: |
| 40 | + self.raw.close() |
| 41 | + self.raw = None |
| 42 | + return self._cached |
| 43 | + |
| 44 | + @property |
| 45 | + def text(self): |
| 46 | + return str(self.content, self.encoding) |
| 47 | + |
| 48 | + def json(self): |
| 49 | + import ujson |
| 50 | + return ujson.loads(self.content) |
| 51 | + |
| 52 | + |
| 53 | +def request(method, url, data=None, json=None, headers={}, stream=None): |
| 54 | + try: |
| 55 | + proto, dummy, host, path = url.split("/", 3) |
| 56 | + except ValueError: |
| 57 | + proto, dummy, host = url.split("/", 2) |
| 58 | + path = "" |
| 59 | + if proto == "http:": |
| 60 | + port = 80 |
| 61 | + elif proto == "https:": |
| 62 | + import ussl |
| 63 | + port = 443 |
| 64 | + else: |
| 65 | + raise ValueError("Unsupported protocol: " + proto) |
| 66 | + |
| 67 | + if ":" in host: |
| 68 | + host, port = host.split(":", 1) |
| 69 | + port = int(port) |
| 70 | + #print("host:",host,",port:",port) |
| 71 | + ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM) |
| 72 | + ai = ai[0] |
| 73 | + |
| 74 | + s = usocket.socket(ai[0], ai[1], ai[2]) |
| 75 | + try: |
| 76 | + s.connect(ai[-1]) |
| 77 | + if proto == "https:": |
| 78 | + s = ussl.wrap_socket(s, server_hostname=host) |
| 79 | + s.write(b"%s /%s HTTP/1.0\r\n" % (method, path)) |
| 80 | + if not "Host" in headers: |
| 81 | + s.write(b"Host: %s\r\n" % host) |
| 82 | + # Iterate over keys to avoid tuple alloc |
| 83 | + for k in headers: |
| 84 | + s.write(k) |
| 85 | + s.write(b": ") |
| 86 | + s.write(headers[k]) |
| 87 | + s.write(b"\r\n") |
| 88 | + if json is not None: |
| 89 | + assert data is None |
| 90 | + import ujson |
| 91 | + data = ujson.dumps(json) |
| 92 | + s.write(b"Content-Type: application/json\r\n") |
| 93 | + if data: |
| 94 | + s.write(b"Content-Length: %d\r\n" % len(data)) |
| 95 | + s.write(b"\r\n") |
| 96 | + if data: |
| 97 | + s.write(data) |
| 98 | + |
| 99 | + l = s.readline() |
| 100 | + #print(l) |
| 101 | + l = l.split(None, 2) |
| 102 | + status = int(l[1]) |
| 103 | + reason = "" |
| 104 | + if len(l) > 2: |
| 105 | + reason = l[2].rstrip() |
| 106 | + while True: |
| 107 | + l = s.readline() |
| 108 | + if not l or l == b"\r\n": |
| 109 | + break |
| 110 | + #print(l) |
| 111 | + if l.startswith(b"Transfer-Encoding:"): |
| 112 | + if b"chunked" in l: |
| 113 | + raise ValueError("Unsupported " + l) |
| 114 | + elif l.startswith(b"Location:") and not 200 <= status <= 299: |
| 115 | + raise NotImplementedError("Redirects not yet supported") |
| 116 | + except OSError: |
| 117 | + s.close() |
| 118 | + raise |
| 119 | + |
| 120 | + resp = Response(s) |
| 121 | + resp.status_code = status |
| 122 | + resp.reason = reason |
| 123 | + return resp |
| 124 | + |
| 125 | + |
| 126 | +def head(url, **kw): |
| 127 | + return request("HEAD", url, **kw) |
| 128 | + |
| 129 | +def get(url, **kw): |
| 130 | + print(">>>get>>>>",url) |
| 131 | + return request("GET", url, **kw) |
| 132 | + |
| 133 | +def post(url, **kw): |
| 134 | + return request("POST", url, **kw) |
| 135 | + |
| 136 | +def put(url, **kw): |
| 137 | + return request("PUT", url, **kw) |
| 138 | + |
| 139 | +def patch(url, **kw): |
| 140 | + return request("PATCH", url, **kw) |
| 141 | + |
| 142 | +def delete(url, **kw): |
| 143 | + return request("DELETE", url, **kw) |
| 144 | + |
| 145 | + |
| 146 | +class Res: |
| 147 | + |
| 148 | + def get(url,file): |
| 149 | + try: |
| 150 | + response = get('https://marty5499.github.io/pythonCode/'+url) |
| 151 | + print(">>",len(response.text) ) |
| 152 | + print("get file:",file,'size:',len(response.text),',save to:',file) |
| 153 | + f = open(file, 'w') |
| 154 | + f.write(response.text) |
| 155 | + f.close() |
| 156 | + print("OK.") |
| 157 | + except Exception as e: |
| 158 | + print("QQ:",e) |
| 159 | + |
| 160 | + |
| 161 | + def exe(dir): |
| 162 | + srcDir = dir |
| 163 | + try: |
| 164 | + while True: |
| 165 | + idx = dir.index('/') |
| 166 | + try: |
| 167 | + name = dir[0:idx] |
| 168 | + try: |
| 169 | + print('mkdir',name) |
| 170 | + os.mkdir(name) |
| 171 | + except: |
| 172 | + pass |
| 173 | + try: |
| 174 | + os.chdir(name) |
| 175 | + print('cd',name) |
| 176 | + except: |
| 177 | + pass |
| 178 | + except: |
| 179 | + pass |
| 180 | + dir = dir[idx+1:] |
| 181 | + except: |
| 182 | + pos = -1 |
| 183 | + try: |
| 184 | + pos = dir.index('.mpy') |
| 185 | + except: |
| 186 | + pass |
| 187 | + try: |
| 188 | + if pos == -1: |
| 189 | + pos = dir.index('.py') |
| 190 | + except: |
| 191 | + pass |
| 192 | + try: |
| 193 | + if pos > 0: |
| 194 | + pyFile = dir |
| 195 | + Res.get(srcDir,pyFile) |
| 196 | + else: |
| 197 | + try: |
| 198 | + print("mkdir",dir) |
| 199 | + os.mkdir(dir) |
| 200 | + except: |
| 201 | + pass |
| 202 | + try: |
| 203 | + os.chdir(dir) |
| 204 | + print("cd ",dir) |
| 205 | + except: |
| 206 | + pass |
| 207 | + except: |
| 208 | + pass |
| 209 | + os.chdir('/') |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +print("connect...") |
| 214 | +do_connect() |
| 215 | +print("get files...") |
| 216 | +Res.exe('lib/webduino.py') |
| 217 | +Res.exe('lib/urequests.py') |
| 218 | +Res.exe('lib/TM1637.py') |
| 219 | +Res.exe('lib/mfrc522.py') |
| 220 | +Res.exe('lib/ssd1306.py') |
| 221 | +Res.exe('lib/st7789py.py') |
| 222 | +Res.exe('lib/umqtt/simple.py') |
| 223 | +Res.exe('lib/max7219.py') |
| 224 | +Res.exe('lib/mled.py') |
| 225 | +Res.exe('lib/ADXL345.py') |
| 226 | +Res.exe('lib/hmc5883l.py') # 三軸加速 |
| 227 | +Res.exe('lib/mlx90614.py') #額溫 |
| 228 | +Res.exe('lib/i2c_lcd.py') #LCD |
| 229 | +Res.exe('lib/uyeelight.py') #LCD |
| 230 | +Res.exe('lib/lcd_api.py') #LCD |
| 231 | +Res.exe('lib/heltec/sx127x.py') #LoRa |
| 232 | +Res.exe('lib/dfplayer.py') #MP3 |
| 233 | +Res.exe('lib/scanplayer.py') #MP3 |
| 234 | +Res.exe('lib/d1motor.py') # d1motor |
| 235 | +Res.exe('lib/webserver/webserver.py') #LoRa |
| 236 | +Res.exe('lib/webserver/index.html') #LoRa |
| 237 | + |
| 238 | +#Res.exe('lib/animations.py') |
| 239 | +#Res.exe('lib/pixelart.py') |
| 240 | +print("========") |
| 241 | +print('Mac address:',ubinascii.hexlify(network.WLAN().config('mac'),':').decode()) |
0 commit comments