network stuff, memory efficient: sockets, recvfrom into a bytearray #11452
Replies: 6 comments 1 reply
-
You can use a pre-allocated buffer and memoryview to access it. |
Beta Was this translation helpful? Give feedback.
-
Hi Robert Sorry, I have to correct myself.
Cpython 3.8 error:
micropython 1.20 error:
Please show me the correct way to assign the result of recvfrom() to the memoryview and how to get how many bytes received. |
Beta Was this translation helpful? Give feedback.
-
BTW, I hit this problem on the esp8266. |
Beta Was this translation helpful? Give feedback.
-
This is a way to reuse a bytearray:
The mydata object will be just a lightweight pointer to the first n bytes of x. |
Beta Was this translation helpful? Give feedback.
-
Problem solved. I get the first packet with recvfrom(), and the next packets with readinto(... fixed buffer ...). |
Beta Was this translation helpful? Give feedback.
-
We need The reference to CPythons Docs: https://docs.python.org/3/library/socket.html#socket.socket.recvfrom_into If the received data does not fit in the
Using the buffer directly, throws a different Exception.
Adding this feature could be used later to optimize |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi
I need to read data from some peers, UDP datagrams, using recvfrom.
To avoid to pollute the memory, I wish to use a bytearray
Example:
Using
len(resp)
to get the length of the payload (data received), it always returns _PKT_SIZE !
I cannot use readinto because the peers, during the communication, change their local ports.
I have to use recvfrom to get their new address.
Real example: TFTP
I am not sure about this line:
resp[0 : ], addr = = client.recvfrom(_PKT_SIZE)
Obviously I cannot use
resp, addr = = client.recvfrom(_PKT_SIZE)
because every time it allocates a new instance of resp
Goals:
Do you have any suggestion?
Beta Was this translation helpful? Give feedback.
All reactions