reading socket into buffer and then reading buffer by line

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
devnull
Posts: 452
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

reading socket into buffer and then reading buffer by line

Post by devnull » Sun Dec 29, 2019 2:38 am

I need to read from the socket and cannot use readline() as it is possible that the data does not contain line endings, in which case it will wait forever.

So I am reading into a byte string as per the code below, however I realise that using a byte string may not be as efficient as reading into a buffer which I have no experience with.

The following code works, but is the method I am using the best way to do this ?? - I need to be able to read by length, line and to pop the data off the buffer after reading it.

Code: Select all

inbuf = b''

def read():
  inbuf += self.sock.read()
  return len(self.inbuf)

def readbuf(size=0,pop=True,line=False):
  os = 0
  if line:
    os=2
    size = inbuf.find(b'\r\n')
    if size < 0: size = 0
  data = inbuf[:size]
  if pop: inbuf = self.inbuf[size+os:]
  return data
Last edited by devnull on Sun Dec 29, 2019 6:36 am, edited 1 time in total.

User avatar
devnull
Posts: 452
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: reading socket into buffer and then reading buffer by line

Post by devnull » Sun Dec 29, 2019 6:23 am

This is an attempt at readline using a byte array:

Code: Select all

a = bytearray(b'0123\r\n45678\r\n9\r\n\r\n')
def readline():
	global a
	data=a
	old=None
	for i in range(len(a)):
		new = a[i]
		if old == 13 and new == 10:
			data = a[:i-1]
			a=a[i+1:]
			return data
		old = new
	a = bytearray()
	return data

>>> readline()
bytearray(b'0123')
>>> readline()
bytearray(b'45678')
>>> readline()
bytearray(b'9')
>>> readline()
bytearray(b'')
>>> 
Same question, is there a better way ??

User avatar
devnull
Posts: 452
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: reading socket into buffer and then reading buffer by line

Post by devnull » Sun Dec 29, 2019 7:19 am

OK, forget these requests for help, I have solved the problem by using select.poll() on the socket.readline()

Post Reply