Skip to content

Instantly share code, notes, and snippets.

@Cheaterman
Created October 11, 2023 12:16
Show Gist options
  • Save Cheaterman/72e63238d485e235097f953f74f2a5ce to your computer and use it in GitHub Desktop.
Save Cheaterman/72e63238d485e235097f953f74f2a5ce 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',
]
# 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
if '\n' not in buffer:
continue
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