Skip to content

Instantly share code, notes, and snippets.

@Cheaterman
Created October 11, 2023 12:25
Show Gist options
  • Save Cheaterman/e4f34280d8cc9f7b5b64dbb5c07197a2 to your computer and use it in GitHub Desktop.
Save Cheaterman/e4f34280d8cc9f7b5b64dbb5c07197a2 to your computer and use it in GitHub Desktop.
main.py
chunks = [
'He',
'llo',
', W',
'orl',
'd!\nA',
'nd h',
'ap',
'py ',
'Pyth',
'oning\n',
'I am a chunk.\nI have three lines.\nOne, two, three.\nYipee!\n',
]
# class that pretends to be your libssh channel
class Channel:
def __init__(self):
self.data = iter(chunks)
def read(self):
try:
return self.data.__next__()
except StopIteration:
return ''
buffer = ''
channel = Channel()
while True:
data = channel.read()
if not data:
# Do you want to process final message if it doesn't have '\n'?
# If so, do it here, before the break.
break
buffer += data
while '\n' in buffer:
message, _, buffer = buffer.partition('\n')
print(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment