From 23c345d61da7d0b763fd04db8259fbd6cfe1ec53 Mon Sep 17 00:00:00 2001 From: Elod Illes Date: Tue, 21 Mar 2023 15:23:06 +0100 Subject: [PATCH] Update serial console example client for py3 A very limited and minimal serial console example client code exists in the documentation [1] for testing purposes. This code was not updated to python 3. This patch updates the example code to be python3 compatible. [1] https://docs.openstack.org/nova/latest/contributor/testing/serial-console.html#testing-the-api Closes-Bug: #2009956 Change-Id: If6617f169d1221b4c43938bdfd37207d079cc7da --- doc/source/contributor/testing/serial-console.rst | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/doc/source/contributor/testing/serial-console.rst b/doc/source/contributor/testing/serial-console.rst index b555f32f29..e5c4c461f9 100644 --- a/doc/source/contributor/testing/serial-console.rst +++ b/doc/source/contributor/testing/serial-console.rst @@ -54,16 +54,20 @@ A simple client for *test purpose* can be written with few lines of Python. while not self.terminated: try: b = self.sock.recv(4096) - sys.stdout.write(b) - sys.stdout.flush() + while len(b) > 0: + # websocket data: opcode + length + data + word = b[2:b[1]+2] + b = b[b[1]+2:] + sys.stdout.buffer.write(word) + sys.stdout.flush() except: # socket error expected pass finally: self.terminate() if __name__ == '__main__': if len(sys.argv) != 2 or not sys.argv[1].startswith("ws"): - print "Usage %s: Please use websocket url" - print "Example: ws://127.0.0.1:6083/?token=xxx" + print("Usage %s: Please use websocket url") + print("Example: ws://127.0.0.1:6083/?token=xxx") exit(1) try: ws = LazyClient(sys.argv[1], protocols=['binary']) @@ -72,7 +76,7 @@ A simple client for *test purpose* can be written with few lines of Python. # keyboard event... c = sys.stdin.read(1) if c: - ws.send(c) + ws.send(c.encode('utf-8'), binary=True) ws.run_forever() except KeyboardInterrupt: ws.close()