Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PYROBUSTA_VERSION := 0.2.0
PYROBUSTA_VERSION := 0.3.0
DEVICE ?= u0

SRC_DIR := src
Expand Down Expand Up @@ -66,14 +66,16 @@ $(BUILD_DIR)/%.py: $(SRC_DIR)/%.py
.PHONY: deploy
deploy:
@echo "Uploading build/$(PKG) to device $(DEVICE)"
@mpremote $(DEVICE) mkdir :/lib || true
@find $(BUILD_DIR)/$(PKG) | while read source; do \
rel=$${source#$(BUILD_DIR)/}; \
remote="/lib/$${rel}"; \
if [ -d "$$source" ]; then \
mpremote $(DEVICE) mkdir "$$rel" || true; \
mpremote $(DEVICE) mkdir "$$remote" || true; \
elif [ -f "$$source" ]; then \
echo "Uploading $$rel"; \
mpremote $(DEVICE) rm "$$rel" || true; \
mpremote $(DEVICE) cp "$$source" ":$$rel"; \
echo "Uploading $$remote"; \
mpremote $(DEVICE) rm ":$$remote" || true; \
mpremote $(DEVICE) cp "$$source" ":$$remote"; \
fi; \
sleep 1; \
done
Expand Down Expand Up @@ -121,10 +123,10 @@ publish:
stage-example:
@echo "Preparing unix runtime in $(RUNTIME_DIR)"
@rm -rf $(RUNTIME_DIR)
@mkdir -p $(RUNTIME_DIR)
@mkdir -p $(RUNTIME_DIR)/lib

@echo "Copying built package"
@cp -r build/pyrobusta $(RUNTIME_DIR)/
@cp -r build/pyrobusta $(RUNTIME_DIR)/lib

@echo "Copying example files"
@cp $(EXAMPLE_DIR)/app.py $(RUNTIME_DIR)/
Expand All @@ -142,7 +144,7 @@ stage-example:
.PHONY: run-unix
run-unix: stage-example
@echo "Running example with unix micropython"
cd $(RUNTIME_DIR) && ../$(MICROPYTHON) app.py
cd $(RUNTIME_DIR) && MICROPYPATH=":.frozen:lib" ../$(MICROPYTHON) app.py

# -----------------------------
# Deploy example app
Expand Down Expand Up @@ -211,9 +213,9 @@ static-checkers: pylint black
.PHONY: stage-test
stage-test:
@rm -rf $(TEST_RUNTIME)
@mkdir -p $(TEST_RUNTIME)
@mkdir -p $(TEST_RUNTIME)/lib

@cp -r build/pyrobusta $(TEST_RUNTIME)/
@cp -r build/pyrobusta $(TEST_RUNTIME)/lib
@cp tests/functional/*.py $(TEST_RUNTIME)/

# -----------------------------
Expand All @@ -225,7 +227,7 @@ test-unix: stage-test tls-cert
@cd $(TEST_RUNTIME); \
for test in test_*.py; do \
echo "Running $$test"; \
../$(MICROPYTHON) $$(basename $$test) || exit 1; \
MICROPYPATH=":.frozen:lib" ../$(MICROPYTHON) $$(basename $$test) || exit 1; \
done

# -----------------------------
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ A lightweight HTTP server library for MicroPython designed for constrained embed
- Routing decorators
- Fixed-size, configurable request/response buffers
- Multipart request and response handling
- Chunked transfer decoding for streamed request bodies
- Bounded-copy memory footprint
- Finite-state-machine parser with linear sliding buffer
- Robust byte-stream handling
Expand Down
Binary file modified dist/pyrobusta/bindings/socket_http.mpy
Binary file not shown.
Binary file modified dist/pyrobusta/con/wifi.mpy
Binary file not shown.
Binary file modified dist/pyrobusta/protocol/http.mpy
Binary file not shown.
Binary file modified dist/pyrobusta/protocol/http_multipart.mpy
Binary file not shown.
Binary file modified dist/pyrobusta/server/http_server.mpy
Binary file not shown.
Binary file modified dist/pyrobusta/stream/buffer.mpy
Binary file not shown.
Binary file modified dist/pyrobusta/transport/socket.mpy
Binary file not shown.
Binary file modified dist/pyrobusta/utils/config.mpy
Binary file not shown.
Binary file added dist/pyrobusta/utils/helpers.mpy
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ to upload it to the root directory of the target device.
| wifi_password | Password of the Wi-Fi network. When empty, Wi-Fi is not initalized by the built-in wifi.py module. | None |
| http_multipart | Enable multipart HTTP requests/responses. | "False" |
| http_mem_cap | Max memory cap (% × 0.01) of usable heap for HTTP request/response stream buffers. | 0.1 |
| http_served_paths | Space delimited list of filesystem paths allowed to be served through HTTP. | "pyrobusta lib" |
| http_served_paths | Space delimited list of filesystem paths allowed to be served through HTTP. | "/lib/pyrobusta" |
| socket_max_con | Max number of socket connections of any enabled application server. | 2 |
| tls | Enable/disable TLS. When turned on, cert.der/key.der must be installed at the root. | "False" |
| log_level | Can be one of: warning, info, debug. | "warning" |
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.2.0",
"version": "0.3.0",
"urls": [
[
"pyrobusta/transport/socket.mpy",
Expand All @@ -9,6 +9,10 @@
"pyrobusta/transport/__init__.py",
"github:szeka9/PyRobusta/dist/pyrobusta/transport/__init__.py"
],
[
"pyrobusta/utils/helpers.mpy",
"github:szeka9/PyRobusta/dist/pyrobusta/utils/helpers.mpy"
],
[
"pyrobusta/utils/__init__.py",
"github:szeka9/PyRobusta/dist/pyrobusta/utils/__init__.py"
Expand Down
17 changes: 7 additions & 10 deletions src/pyrobusta/bindings/socket_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ def init_pools(max_sockets):
)
if is_low_memory:
logging.warning(
(
"[SocketHttp.init_pools] low-memory mode with reduced buffer size, "
"decrease max_clients to use larger buffers"
)
__name__ + ".init_pools: low-memory mode with reduced buffer size"
)
recv_size = (
SocketHttp.RECV_BUF_MIN_BYTES
Expand All @@ -73,13 +70,13 @@ def init_pools(max_sockets):
if usable < per_conn:
raise MemoryError(
(
f"Insufficient memory for webserver: {mem_available // 1024} KB "
f"Insufficient memory: {mem_available // 1024} KB "
f"at {SocketHttp.MEM_CAP*100}% cap, "
f"at least {per_conn // 1024} KB required"
)
)
con_limit = min(usable // per_conn, con_limit)
logging.info((f"[SocketHttp.init_pools] {con_limit} connection(s) allowed"))
logging.info((__name__ + f".init_pools: {con_limit} connection(s) allowed"))
SocketHttp.RECV_POOL = MemoryPool(recv_size, con_limit, wrapper=SlidingBuffer)
SocketHttp.SEND_POOL = MemoryPool(send_size, con_limit, wrapper=SlidingBuffer)

Expand Down Expand Up @@ -113,7 +110,7 @@ async def run(self):
await self._run_state_machine()
await sleep_ms(SocketHttp.STATE_MACHINE_SLEEP_MS)
except Exception as e: # pylint: disable=W0718
logging.warning(f"[SocketHttp] error in run_web: {e}")
logging.warning(__name__ + f": error in run_web: {e}")
finally:
if self._send_buf:
self._send_buf.consume()
Expand All @@ -126,7 +123,7 @@ async def run(self):

async def _reserve_buffers(self):
if SocketHttp.SEND_POOL is None or SocketHttp.RECV_POOL is None:
raise RuntimeError("Buffer pools are uninitialized")
raise RuntimeError("Pools are ninitialized")

while not self._recv_buf or not self._send_buf:
if not self._recv_buf:
Expand Down Expand Up @@ -158,7 +155,7 @@ async def _run_state_machine(self):
await self._flush_response()
return
except Exception as e: # pylint: disable=W0718
logging.warning(f"[SocketHttp] error in _run_state_machine: {e}")
logging.warning(__name__ + f"._run_state_machine: {e}")
self._engine.on_failure(self._send_buf, str(e).encode("ascii"))
await self._flush_response()
return
Expand Down Expand Up @@ -188,7 +185,7 @@ async def _read_to_buf(self):
await self._flush_response()
return 0
self._recv_buf.write(request)
logging.debug(f"[SocketHttp._read_to_buf] read new message chunk: {request}")
logging.debug(__name__ + f"._read_to_buf: [{request}]")
return len(request)

async def _response_handler(self, resp_handler):
Expand Down
6 changes: 3 additions & 3 deletions src/pyrobusta/con/wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ def initialize():
ssid = get_config("wifi_ssid")
password = get_config("wifi_password")
if not ssid or not password:
logging.warning("[Wi-Fi] Missing SSID/password, skip Wi-Fi initialization")
logging.warning(__name__ + ": missing SSID/password, skip initialization")
return

sta_if = WLAN(STA_IF)
sta_if.active(True)
nets = sta_if.scan()
for net in nets:
if net[0].decode() == get_config("wifi_ssid"):
logging.info(f"[Wi-Fi] Network {net[0]} found!")
logging.info(__name__ + f": network {net[0]} found!")
sta_if.connect(net[0], get_config("wifi_password"))
logging.info("[Wi-Fi] WLAN connection succeeded!")
logging.info(__name__ + f": connected, available at {sta_if.ifconfig()[0]}")
break


Expand Down
Loading
Loading