-
Notifications
You must be signed in to change notification settings - Fork 78
ZephyrSocket
Currently, building MicroPython Zephyr port with network sockets support requires special branches both for Zephyr and for MicroPython:. All Zephyr changes are merged upstream. (zephyr-socket2 branch is gone, zephyr-wip is used now.)
-
Use Zephyr master.git clone https://github.com/pfalcon/zephyr; git checkout master-micropython
. Usegit pull --rebase
to update (branch rebased). -
git clone https://github.com/pfalcon/micropython; git checkout zephyr-wip
. Usegit pull --rebase
to update (branch rebased). - Follow https://www.zephyrproject.org/doc/subsystems/networking/qemu_setup.html to bring up QEMU TAP/SLIP based local networking. To access Internet, you will need to setup NAT rules as described there too.
- Setup Zephyr environment for
master-micropython
per its docs. cd micropython/zephyr; make
-
make run
. If it doesn't start up, check your step 3. - From another terminal,
ping 192.0.2.1
. If pings don't come thru, check your step 3. - Switch back to QEMU terminal from step 6, and start playing!
Sample code, connecting to HTTP server on your local machine (Apache runs by default on many Linux machines, if not, install it):
import socket
s = socket.socket()
s.connect(("192.0.2.2", 80))
s.send(b"GET /foo HTTP/1.0\r\n\r\n")
s.recv(1000)
For further testing, DNS server/proxy running on the host (192.0.2.2) is required. A typical Ubuntu system runs dnsmasq
proxy, but it doesn't pick up newly created network interfaces, so you need to restart it explicitly:
service dnsmasq restart
Also, make sure that you set up masquerading iptable rules to allow access to Internet from Zephyr/QEMU: https://www.zephyrproject.org/doc/subsystems/networking/qemu_setup.html#setting-up-nat-masquerading-to-access-internet
To test large (multi-megabyte) HTTP downloads from a real Internet server, run:
import test_dl
This will download, in loop, large file from http://archive.ubuntu.com, and check hash checksum of each attempt.
To test HTTP server implemented in MicroPython using Zephyr sockets API, run:
import http_server
Then from host, access http://192.0.2.1:8080/ with a browser. The expected outcome is a page with text content:
Hello #0 from MicroPython!
with number incrementing on each page reload (may be not consecutive, as modern browser routinely issue more requests than asked by a user).
For more thorough testing, run Apache Bench:
ab -n1000 http://192.0.2.1:8080/
This will issue 1000 consecutive requests to the sample server. The expected outcome (shortened) is:
Complete requests: 1000
Failed requests: 992
(Connect: 0, Receive: 0, Length: 992, Exceptions: 0)
Total transferred: 45896 bytes
HTML transferred: 28896 bytes
"Failed requests" are due to the fact that http_server
generates dynamically changes content, while ab
expected a static page. Make sure that only errors are "Length" ones.
Note: With a larger number of requests, e.g. 10000, there's a growing chance that http_server will fail. The likely cause is some race condition or non-POSIX handling of some edge condition on Zephyr side. It's subject for future investigation.