Created
October 11, 2023 12:25
-
-
Save Cheaterman/e4f34280d8cc9f7b5b64dbb5c07197a2 to your computer and use it in GitHub Desktop.
main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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