Merge "Update serial console example client for py3"

This commit is contained in:
Zuul
2023-08-31 21:02:32 +00:00
committed by Gerrit Code Review

View File

@@ -54,16 +54,20 @@ A simple client for *test purpose* can be written with few lines of Python.
while not self.terminated: while not self.terminated:
try: try:
b = self.sock.recv(4096) b = self.sock.recv(4096)
sys.stdout.write(b) while len(b) > 0:
sys.stdout.flush() # 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 except: # socket error expected
pass pass
finally: finally:
self.terminate() self.terminate()
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) != 2 or not sys.argv[1].startswith("ws"): if len(sys.argv) != 2 or not sys.argv[1].startswith("ws"):
print "Usage %s: Please use websocket url" print("Usage %s: Please use websocket url")
print "Example: ws://127.0.0.1:6083/?token=xxx" print("Example: ws://127.0.0.1:6083/?token=xxx")
exit(1) exit(1)
try: try:
ws = LazyClient(sys.argv[1], protocols=['binary']) 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... # keyboard event...
c = sys.stdin.read(1) c = sys.stdin.read(1)
if c: if c:
ws.send(c) ws.send(c.encode('utf-8'), binary=True)
ws.run_forever() ws.run_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
ws.close() ws.close()