Skip to content

Conversation

@dpgeorge
Copy link
Member

@dpgeorge dpgeorge commented Aug 3, 2024

Summary

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.

And use SSLContext instead of ssl.wrap_socket() because the latter is deprecated (in CPython at least).

Fixes issue #15580.

Testing

Tested on CPython (only http_client.py and https_client.py), MicroPython unix and Pico W.

@dpgeorge dpgeorge added the examples Relates to examples/ directory in source label Aug 3, 2024
@codecov
Copy link

codecov bot commented Aug 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.43%. Comparing base (d9a0fdd) to head (fd03a05).
Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #15588   +/-   ##
=======================================
  Coverage   98.43%   98.43%           
=======================================
  Files         163      163           
  Lines       21295    21295           
=======================================
  Hits        20961    20961           
  Misses        334      334           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dpgeorge dpgeorge force-pushed the examples-network-improve-client-examples branch from 548a00d to 242b47a Compare August 3, 2024 06:47
@dpgeorge dpgeorge force-pushed the examples-network-improve-client-examples branch from 242b47a to 20952c7 Compare August 14, 2024 01:45
@dpgeorge dpgeorge requested a review from projectgus August 14, 2024 01:45
Copy link
Contributor

@projectgus projectgus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes look good!

The only other thing that I'd suggest is to add an extra comment along these lines to both examples:

# To implement an HTTP client using less code, use mip to install the requests package:
# https://github.com/micropython/micropython-lib/tree/master/python-ecosys/requests

@dpgeorge dpgeorge force-pushed the examples-network-improve-client-examples branch from 20952c7 to 1a0f9ac Compare August 27, 2024 04:48
@dpgeorge
Copy link
Member Author

Thanks for the review. I've added that comment to the HTTP and HTTPS client examples (not the non-blocking one, because that doesn't generalise to requests).

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 micropython#15580.

Signed-off-by: Damien George <damien@micropython.org>
`ssl.wrap_socket()` is deprecated in CPython, so use `SSLContext` instead,
so the example is a good example to copy.

Signed-off-by: Damien George <damien@micropython.org>
Not just the domain name.  This gives better HTTP 1.0 examples if someone
wants to copy them.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the examples-network-improve-client-examples branch from 1a0f9ac to fd03a05 Compare August 28, 2024 02:29
@dpgeorge dpgeorge merged commit fd03a05 into micropython:master Aug 28, 2024
@dpgeorge dpgeorge deleted the examples-network-improve-client-examples branch August 28, 2024 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples Relates to examples/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants