jczic / MicroWebSrv2

The last Micro Web Server for IoTs (MicroPython) or large servers (CPython), that supports WebSockets, routes, template engine and with really optimized architecture (mem allocations, async I/Os). Ready for ESP32, STM32 on Pyboard, Pycom's chipsets (WiPy, LoPy, ...). Robust, efficient and documented!
https://github.com/jczic/MicroWebSrv2
MIT License
659 stars 97 forks source link

'module' object has no attribute 'SSLSocket' #95

Open Poly-Mentor opened 11 months ago

Poly-Mentor commented 11 months ago

Hey, firstly - thank you for the great job writing this library, it's very easy to use. I used it in some of my projects before. However I have encountered a problem in my recent project, trying to run this server on ESP32, or to be more precise - Wemos S2 mini. In my main.py i run standard setup:


CONTENT = f"<h1>Hello, World!</h1><br>{values}"

@WebRoute(GET, '/test')
def RequestTest(microWebSrv2, request) :
    request.Response.ReturnOk(content=CONTENT)

@WebRoute(GET, '/script.js')
def RequestTest(microWebSrv2, request) :
    request.Response.ReturnFile("/www/script.js", None)

@WebRoute(GET, '/style.css')
def RequestTest(microWebSrv2, request) :
    request.Response.ReturnFile("/www/style.css", None)
mws2 = MicroWebSrv2()
mws2.SetEmbeddedConfig()
mws2.StartManaged()

It is initialized correctly, but when I try to access device's IP via browser an exception is raised:

       ---------------------------
       - Python pkg MicroWebSrv2 -
       -      version 2.0.6      -
       -     by JC`zic & HC2     -
       ---------------------------

 + [@WebRoute] GET /test
 + [@WebRoute] GET /script.js
 + [@WebRoute] GET /style.css
MWS2-INFO> Server listening on 0.0.0.0:80.
MWS2-INFO> Starts the managed pool to wait for I/O events.
Running...
Unhandled exception in thread started by <bound_method>
Traceback (most recent call last):
  File "MicroWebSrv2/libs/XAsyncSockets.py", line 131, in _processWaitEvents
  File "MicroWebSrv2/libs/XAsyncSockets.py", line 587, in OnReadyForReading
  File "MicroWebSrv2/libs/XAsyncSockets.py", line 830, in IsSSL
AttributeError: 'module' object has no attribute 'SSLSocket'

it points to those lines of code from XAsyncSockets.py

    @property
    def IsSSL(self) :
        return ( hasattr(ssl, 'SSLContext') and \
                 isinstance(self._socket, ssl.SSLSocket) )

when I add debugging print statement to this method

    @property
    def IsSSL(self) :
        print(f"{ssl=} {type(ssl)=} {dir(ssl)=}")
        return ( hasattr(ssl, 'SSLContext') and \
                 isinstance(self._socket, ssl.SSLSocket) )

that's the result:

MWS2-INFO> Server listening on 0.0.0.0:80.
MWS2-INFO> Starts the managed pool to wait for I/O events.
Running...
ssl=<module 'ssl'> type(ssl)=<class 'module'> dir(ssl)=['__class__', '__name__', '__dict__', 'CERT_NONE', 'CERT_OPTIONAL', 'CERT_REQUIRED', 'PROTOCOL_TLS_CLIENT', 'PROTOCOL_TLS_SERVER', 'SSLContext', 'wrap_socket']
Unhandled exception in thread started by <bound_method>
Traceback (most recent call last):
  File "MicroWebSrv2/libs/XAsyncSockets.py", line 131, in _processWaitEvents
  File "MicroWebSrv2/libs/XAsyncSockets.py", line 587, in OnReadyForReading
  File "MicroWebSrv2/libs/XAsyncSockets.py", line 831, in IsSSL
AttributeError: 'module' object has no attribute 'SSLSocket'

Unfortunately I don't have a knowledge to figure out if ssl module is valid and why there's 'SSLSocket' attribute missing. Can you help me?

EDIT: I also tried deleting second condition from "and" expression to see what happens next

    @property
    def IsSSL(self) :
        print(f"{ssl=} {type(ssl)=} {dir(ssl)=}")
        return hasattr(ssl, 'SSLContext')

But i got an error futher in this part of the same file:

                            if self.IsSSL and self._socket.pending() > 0 :
                                break
Traceback (most recent call last):
  File "MicroWebSrv2/libs/XAsyncSockets.py", line 131, in _processWaitEvents
  File "MicroWebSrv2/libs/XAsyncSockets.py", line 587, in OnReadyForReading
AttributeError: 'socket' object has no attribute 'pending'
ekondayan commented 10 months ago

@ParryHotter95

I have the exact same issue on a ESP32 chip and I think that there is a bug there on line 830.

Maybe this RFC have something in common: https://github.com/micropython/micropython/issues/8915

Changing ssl.SSLSocket to ssl.SSLContext on line 830 fixes the issue.

ekondayan commented 10 months ago

@ParryHotter95

You can see a PR here: https://github.com/jczic/MicroWebSrv2/pull/98