Skip to content

windows: Implement socket/ssl modules - #12810

Open
stinos wants to merge 9 commits into
micropython:masterfrom
stinos:winsock
Open

stinos wants to merge 9 commits into
micropython:masterfrom
stinos:winsock

Conversation

@stinos

@stinos stinos commented Oct 26, 2023 •

Copy link
Copy Markdown
Contributor

Socket module is enabled for mingw and msvc builds, mbedtls only for the former; for no particular reason, I can add it there as well if that makes more sense.

As for testing I manually ran the relevant multi_net/net_inet/net_hosted tests and tried the requests module both with and without SSL, and all of that works fine so it's likely that everything is done correctly.

The new multi_net/tcp_recv_timeout.py fails sometimes on ci, likely because one of the recv() calls errors out because the remote disconnected already, which I thought multitest.next() would make sure didn't happen but I probably misunderstood what it does. Someone has an idea for that? I just want the recv calls on both ends to time out.

The test fixes with the numeric values I'm not super happy about, but the only proper way of doing this is to provide common functions to check these errors in a test_helpers module or something like that, and then perhaps implementing a complete errno module for windows which does include all WSAXXX error code, which is a bit much maybe?

@codecov

codecov Bot commented Oct 26, 2023 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.43%. Comparing base (185116e) to head (1f4266d).
⚠️ Report is 2150 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #12810   +/-   ##
=======================================
  Coverage   98.43%   98.43%           
=======================================
  Files         163      163           
  Lines       21290    21290           
=======================================
  Hits        20956    20956           
  Misses        334      334           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@projectgus

Copy link
Copy Markdown
Contributor

This is an automated heads-up that we've just merged a Pull Request
that removes the STATIC macro from MicroPython's C API.

See #13763

A search suggests this PR might apply the STATIC macro to some C code. If it
does, then next time you rebase the PR (or merge from master) then you should
please replace all the STATIC keywords with static.

Although this is an automated message, feel free to @-reply to me directly if
you have any questions about this.

@github-actions

github-actions Bot commented Mar 25, 2024 •

Copy link
Copy Markdown

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS

@stinos
stinos force-pushed the winsock branch 4 times, most recently from 0aae1b9 to dc44e7c Compare March 25, 2024 13:48
@stinos

stinos commented Mar 25, 2024

Copy link
Copy Markdown
Contributor Author

Apart from the STATIC changes, this went rather stale all recent mbedtls changes as well, and the switch from Appveyor to Github Actions; I updated everything now. The 2 mingw standard variant builds currently fail, but there's something weird going on with how Github Actions runs these builds and I'm still looking into that.

@stinos

stinos commented Mar 26, 2024

Copy link
Copy Markdown
Contributor Author

The 2 mingw standard variant builds currently fail, but there's something weird going on with how Github Actions runs these builds and I'm still looking into that.

Fix in #14188

stinos added 9 commits August 20, 2024 15:31
Note these are mostly the 'bare' socket tests, not the ssl/tls ones
for instance: most of these don't run on CPython because of
incompatible wrap_socket() arguments.
The change mostly consists of checking the WSA error codes next to
the errno ones and these are written as numeric values because the
names (like WSAEAGAIN) are only available in CPython and not in
micropython.

Signed-off-by: stijn <[email protected]>
Upcoming commits are going to port this file to work with sockets
on windows so prepare for that with the platform-specific bits.

Signed-off-by: stijn <[email protected]>
This is more consistent with the Makefile/CMake-based builds.

Signed-off-by: stijn <[email protected]>
This is consistent with extmod.mk's split between SRC_EXTMOD_C and
SRC_THIRDPARTY_C, the latter specifying source files which get
compiled but not included in qstr generation.

Signed-off-by: stijn <[email protected]>
Supports preprocessor definitions like /Dval="quotedvalue" by turning
that into /Dval=\"quotedvalue\".

Signed-off-by: stijn <[email protected]>
@systematicguy

Copy link
Copy Markdown

So, during the EuroPython 2026 @jonnor has organized a sprint on MicroPython.
I had a look into this stale PR.

I have forked micropython and rebased the branch of @stinos onto a fresh master and it is ready to be picked up by whoever wants to push this further:
https://github.com/systematicguy/micropython/tree/winsock

There were some conflicts, all of them straightforward.

It works, has been tested using the standard PyVariant using this file:

# https_test.py
import socket
import ssl

host = "api.open-meteo.com"
addr = socket.getaddrinfo(host, 443)[0][-1]

sock = socket.socket()
try:
    sock.connect(addr)

    sock = ssl.wrap_socket(sock, server_hostname=host)
    sock.write(
        b"GET /v1/forecast?latitude=59.91&longitude=10.75&current=temperature_2m HTTP/1.0\r\nHost: %s\r\n\r\n" % host.encode()
    )
    print(sock.read(600))
finally:
    sock.close()

Steps to build and test on windows:

cd micropython\ports\windows
msbuild "..\..\mpy-cross\mpy-cross.vcxproj" /p:Configuration=Release /p:Platform=x64 /p:PyVariant=standard
msbuild micropython.vcxproj /t:UpdateSubmodules
msbuild micropython.vcxproj /p:Configuration=Release /p:Platform=x64
# minimal smoketest:
.\build-standard\Releasex64\micropython.exe -c "import socket, ssl; print('socket+ssl OK')"
# actual test:
.\build-standard\Releasex64\micropython.exe https_test.py

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants