summaryrefslogtreecommitdiff
path: root/examples/network/https_client_nonblocking.py
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2024-08-03 16:06:52 +1000
committerDamien George <damien@micropython.org>2024-08-28 12:28:58 +1000
commitc8385ef75aea929e7175173d3190dd787f37c8e4 (patch)
tree538f2fc0aefd4cece62cb83e76cd1b317cdf7838 /examples/network/https_client_nonblocking.py
parentd9a0fdda9a7b0db55c1115b55bb1b83cd5ce739c (diff)
examples/network: Support IPv4 and IPv6 in HTTP client examples.
The main changes here are to pass the address family and socket type to `getaddrinfo()`, and then use the result of the address lookup when creating the socket, so it has the correct address family. This allows both IPv4 and IPv6 to work, because the socket is created with the correct AF_INETx type for the address. Also add some more comments to the examples to explain what's going on. Fixes issue #15580. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'examples/network/https_client_nonblocking.py')
-rw-r--r--examples/network/https_client_nonblocking.py4
1 files changed, 2 insertions, 2 deletions
diff --git a/examples/network/https_client_nonblocking.py b/examples/network/https_client_nonblocking.py
index 6e6b7f37b..41447e81e 100644
--- a/examples/network/https_client_nonblocking.py
+++ b/examples/network/https_client_nonblocking.py
@@ -37,13 +37,13 @@ def read_nonblocking(poller, sock, n):
return data
-def main(url):
+def main(url, addr_family=0):
# Split the given URL into components.
proto, _, host, path = url.split(b"/", 3)
assert proto == b"https:"
# Note: this getaddrinfo() call is blocking!
- ai = socket.getaddrinfo(host, 443)[0]
+ ai = socket.getaddrinfo(host, 443, addr_family, socket.SOCK_STREAM)[0]
addr = ai[-1]
print("Connect address:", addr)