socket differences between MicroPython and CPython
Posted: Wed Feb 19, 2020 12:13 pm
Hello,
I'm currently working on a project to be deployed on EP8266.
For easiness of debug I'm developing and test all none-hardware dependent on Windows + CPython 3.7.5 + VSC
Note that while having quite a some years of embedded and desktop programming, I started Python and MicroPython less than a year ago
One of the topic I am struggling with is socket differences between MicroPython and CPython.
Here are some of my wandering
1) `readline()` is not available on CPython's socket so I must do a `makefile()` to get a file handler. This code works fine in both MicroPython and CPython but I am wondering
- Can I keep both the socket and the stream and use alternatively one or the other ?
- Any risk ?
- Any memory drawback for MicroPython ? (apart for storing the stream along with the socket)
2) Micropython `readline()` returns a `str` while CPython returns a `bytearray` that need to be `decode()`-ed. So I have added code to test `if not isinstance(buffer,(str))` to decide if I have to `decode()` or not
As I fear that all those tests will increase the code to be run under MicroPython, is there a better way ?
3) Micropython socket `send()` and `sendall()` can accept either `str` or `bytearray` while CPython only accept `bytearray`.
Here also I would appreciate your advices on how I should code such in order to minimize the impact when it has to run on MicroPython on ESP8266.
Thank a lot for your advices
Best regards
I'm currently working on a project to be deployed on EP8266.
For easiness of debug I'm developing and test all none-hardware dependent on Windows + CPython 3.7.5 + VSC
Note that while having quite a some years of embedded and desktop programming, I started Python and MicroPython less than a year ago
One of the topic I am struggling with is socket differences between MicroPython and CPython.
Here are some of my wandering
1) `readline()` is not available on CPython's socket so I must do a `makefile()` to get a file handler. This code works fine in both MicroPython and CPython but I am wondering
- Can I keep both the socket and the stream and use alternatively one or the other ?
- Any risk ?
- Any memory drawback for MicroPython ? (apart for storing the stream along with the socket)
2) Micropython `readline()` returns a `str` while CPython returns a `bytearray` that need to be `decode()`-ed. So I have added code to test `if not isinstance(buffer,(str))` to decide if I have to `decode()` or not
As I fear that all those tests will increase the code to be run under MicroPython, is there a better way ?
3) Micropython socket `send()` and `sendall()` can accept either `str` or `bytearray` while CPython only accept `bytearray`.
Here also I would appreciate your advices on how I should code such in order to minimize the impact when it has to run on MicroPython on ESP8266.
Thank a lot for your advices
Best regards