mpy usocket.readinto == cpy socket.recv_into ?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
PinkInk
Posts: 65
Joined: Tue Mar 11, 2014 3:42 pm

mpy usocket.readinto == cpy socket.recv_into ?

Post by PinkInk » Wed Aug 17, 2016 3:03 am

I think MicroPython's usocket.readinto method does the same thing as cpython's socket.recv_into. Hence for testing in cpython, before deploying to esp or WiPy, I've been doing this at import time;

Code: Select all

try: 
     import usocket as socket 
 except: 
     import socket 
     socket.socket.readinto = socket.socket.recv_into 
... and it seems to work OK, but am I missing some subtle difference between the two?

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: mpy usocket.readinto == cpy socket.recv_into ?

Post by pfalcon » Wed Aug 17, 2016 12:55 pm

No, micropython's socket.readinto() is equivalent to socket.makefile("rwb").readinto() and is a shortcut for the latter. recv() and read() have differences in semantics, see https://github.com/micropython/micropython/issues/2056 and comments in https://github.com/micropython/micropyt ... es/network
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

PinkInk
Posts: 65
Joined: Tue Mar 11, 2014 3:42 pm

Re: mpy usocket.readinto == cpy socket.recv_into ?

Post by PinkInk » Thu Aug 18, 2016 12:59 am

Ok, got you - didn't understand how you got there but the comments in the http server example make sense. Thanks.

This was in the context of an http server reading files from flash and serving them up over http - is there a way to negate the need for an intermediary Python buffer object and read from a file directly into a socket?

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: mpy usocket.readinto == cpy socket.recv_into ?

Post by pfalcon » Sat Sep 17, 2016 5:04 pm

No. Not in a general case.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Post Reply