File tree 1 file changed +12
-3
lines changed
1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -161,22 +161,31 @@ def recv(self, bufsize=0):
161
161
gc .collect ()
162
162
return ret
163
163
164
- def recv_into (self , buffer ):
164
+ def recv_into (self , buffer , nbytes = 0 ):
165
165
"""Read some bytes from the connected remote address into a given buffer
166
166
167
167
:param bytearray buffer: The buffer to read into
168
+ :param int nbytes: (Optional) Number of bytes to receive default is 0,
169
+ which will receive as many bytes as possible before filling the
170
+ buffer or timing out
168
171
"""
169
172
173
+ if not 0 <= nbytes <= len (buffer ):
174
+ raise ValueError (
175
+ "Can only read number of bytes between 0 and length of supplied buffer"
176
+ )
177
+
170
178
stamp = time .monotonic ()
171
179
to_read = len (buffer )
180
+ limit = 0 if nbytes == 0 else to_read - nbytes
172
181
received = []
173
- while to_read > 0 :
182
+ while to_read > limit :
174
183
# print("Bytes to read:", to_read)
175
184
avail = self .available ()
176
185
if avail :
177
186
stamp = time .monotonic ()
178
187
recv = _the_interface .socket_read (self ._socknum , min (to_read , avail ))
179
- # received.append(recv)
188
+ received .append (recv )
180
189
start = len (buffer ) - to_read
181
190
to_read -= len (recv )
182
191
end = len (buffer ) - to_read
You can’t perform that action at this time.
0 commit comments