Skip to content

Instantly share code, notes, and snippets.

@saknarak
Created June 20, 2021 15:43
Show Gist options
  • Save saknarak/c5bd415b8e7f8cefe03d3560ba4e7adf to your computer and use it in GitHub Desktop.
Save saknarak/c5bd415b8e7f8cefe03d3560ba4e7adf to your computer and use it in GitHub Desktop.
micropython http client
import ujson as json
import uos as os
import usocket as socket
# original from https://gist.github.com/hiway/b3686a7839acca7d62e3a7234fdbb438
def http_get_async(url):
_, _, host, path = url.split('/', 3)
if ':' in host:
host, port = host.split(':')
else:
port = 80
addr = socket.getaddrinfo(host, port)[0][-1]
s = socket.socket()
s.settimeout(1) # 1sec
s.connect(addr)
s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\nConnection: Close\r\n\r\n' % (path, host), 'utf8'))
prev_data = b''
endHeader = False
while True:
data = s.recv(1000)
if data:
print("data=", data)
if endHeader:
yield data
continue
data = prev_data + data
if b'\r\n\r\n' in data:
endHeader = True
_, body = data.split(b'\r\n\r\n', 1)
yield body
continue
else:
prev_data = data
else:
print("NO MORE DATA")
break
def http_get(url):
response = b''
try:
get = http_get_async(url)
while True:
file_bytes = get.send(None)
response += file_bytes
except StopIteration:
pass
response_str = str(response, 'utf-8')
return response_str
def ensure_dirs(path):
split_path = path.split('/')
if len(split_path) > 1:
for i, fragment in enumerate(split_path):
parent = '/'.join(split_path[:-i])
try:
os.mkdir(parent)
except OSError:
pass
def http_get_to_file(url, path):
ensure_dirs(path)
with open(path, 'w') as outfile:
try:
get = http_get_async(url)
while True:
file_bytes = get.send(None)
outfile.write(file_bytes)
except StopIteration:
outfile.close()
def _start(url='http://192.168.1.107:5000/esp8266/files.json'):
response = json.loads(http_get(url))
for file in response['files']:
path = file['path']
url = 'http://192.168.1.107:5000/esp8266/{}'.format(path)
http_get_to_file(url, path)
if __name__ == '__main__':
_start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment