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
socket differences between MicroPython and CPython
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: socket differences between MicroPython and CPython
Have you considered using the Unix build of MicroPython for testing? This enables you to use MicroPython code throughout.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: socket differences between MicroPython and CPython
Hi Peter
Very sorry for late response. I was expecting the forum to notify me of your message. I may have missed a setting.
If I get it right, I need to build it from the sources.
Is that as easy as downloading the github zip and run make from the ports/unix folder ?
Is there any dependencies on not so standard libraries I should install ?
Thanks for your response
Best regards
Very sorry for late response. I was expecting the forum to notify me of your message. I may have missed a setting.
If I get it right, I need to build it from the sources.
Is that as easy as downloading the github zip and run make from the ports/unix folder ?
Is there any dependencies on not so standard libraries I should install ?
Thanks for your response
Best regards
Re: socket differences between MicroPython and CPython
Yes.
I'd recommend cloning the repo rather than downloading the zip.
But yes
Code: Select all
cd mpy-cross
make
cd ../ports/unix
make submodules
make
Re: socket differences between MicroPython and CPython
Thanks for your help
I'll give it a go and report back
I'll give it a go and report back