socket differences between MicroPython and CPython

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
barbudor
Posts: 6
Joined: Fri Feb 08, 2019 6:35 pm

socket differences between MicroPython and CPython

Post by barbudor » 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

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: socket differences between MicroPython and CPython

Post by pythoncoder » Thu Feb 20, 2020 7:27 am

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.

barbudor
Posts: 6
Joined: Fri Feb 08, 2019 6:35 pm

Re: socket differences between MicroPython and CPython

Post by barbudor » Wed Apr 08, 2020 5:35 pm

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

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: socket differences between MicroPython and CPython

Post by jimmo » Thu Apr 09, 2020 12:30 am

barbudor wrote:
Wed Apr 08, 2020 5:35 pm
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 ?
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
Other than just regular GCC, you might need libffi-dev installed by your system package manager. I'm pretty sure that's it.

barbudor
Posts: 6
Joined: Fri Feb 08, 2019 6:35 pm

Re: socket differences between MicroPython and CPython

Post by barbudor » Thu Apr 09, 2020 4:53 pm

Thanks for your help
I'll give it a go and report back

Post Reply