Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ParryHotter95 opened this issue Oct 13, 2023 · 2 comments
Open

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

ParryHotter95 opened this issue Oct 13, 2023 · 2 comments

Comments

@ParryHotter95
Copy link

ParryHotter95 commented Oct 13, 2023

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
Copy link

ekondayan commented Nov 6, 2023

@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: micropython/micropython#8915

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

@ekondayan
Copy link

ekondayan commented Nov 7, 2023

@ParryHotter95

You can see a PR here: #98

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants