Pythonista-Issues icon indicating copy to clipboard operation
Pythonista-Issues copied to clipboard

Websocket and ssl modules not working

Open jametron opened this issue 6 years ago • 3 comments
trafficstars

I am trying to send remote control commands to my Samsung TV via websockets (see example). On my Mac I have succeeded in doing this using the module Websocket as well as ssl for disabling ssl certificate verification. The only lines of code requiring either of these modules are:

ws = websocket.create_connection(strURL, sslopt={'cert_reqs' : ssl.CERT_NONE}) ws.send(strJson)

In Pythonista on my phone, however, neither of these modules appear to run correctly and I'm not at the stage where I can identify the root cause. I also tried websockets and aiohttp, neither successfully. All of these modules I've had to install using staSh; sometimes the installation throws some warnings or errors, but I'm not sure of the impact on their functionality

I am initially just putting this out there to see if anyone knows of a reason why, fundamentally, all of the modules I have tried so far aren't working and whether there is a workaround or an alternative that does work. If you need more info on the code and the error messages, I can provide that.

Thanks for your help!

jametron avatar Nov 18 '19 07:11 jametron

Share the trace you get?

mikaelho avatar Nov 18 '19 17:11 mikaelho

I have successfully used websockets. (Note the ”s” at the end.)

mikaelho avatar Nov 18 '19 17:11 mikaelho

Thanks @mikaelho, I was going to try websockets again next, but I have now resolved the issue by uninstalling ssl and substituting ss.CERT_NONE with 0 when creating the websocket connection. In addition I had to make some changes to the installation of websocket due to Numpy version limitations in Pythonista.

Here are more details below:

  1. I got the following error in staSh when doing pip install ssl:

Running setup file ... SyntaxError('invalid syntax', ('/private/var/mobile/Containers/Data/Application/CB4ACF17-B8E0-4890-A439-E8D8644E1153/tmp/ce3dc47463a942e49afea35f45ff4ebb/ssl-1.16/setup.py', 33, 27, " print 'looking for', f\n")) Failed to run setup.py Fall back to directory guessing ... Package installed: ssl

  1. My code was falling over when importing websocket with the error: module 'six' has no attribute 'PY34'. There really doesn't appear to be an attribute six.PY34! Once I removed this issue I got the next one...

  2. Several lines of the file ssl/init.py were styled as except SSLError, x:, which was throwing a syntax error because it should be except SSLError as x:, right? Right?! So I changed those lines too and got the next error...

  3. Still trying to import websocket I now had an error in the line from ._ssl_compat import *: AttributeError: module 'websocket._ssl_compat' has no attribute 'ssl'. Not sure about this one, because _ssl_compat.py exists in the websocket folder.

Anyway at this point I tried skipping over import websocket to see if the next line, import ssl worked... and it didn't. It fell over trying to import _ssl2 and I noticed that there was a file _ssl2.c in the ssl folder. QUESTION: Is any module that is dependent on such .c files not going to work in Pythonista?

  1. I uninstalled ssl and now import websocket ran without error! Instead I got an error when running the line ws = websocket.create_connection(... because it was trying to use a Numpy method .tobytes() which was new in 1.9.0, and I understand that this is newer than the native Numpy installation in Pythonista. I could get around this easily by editing a line in the affected script so that it doesn't depend on Numpy at all. AND THEN IT WORKED!

jametron avatar Nov 19 '19 07:11 jametron