Skip to content

Instantly share code, notes, and snippets.

@sahid
Created September 26, 2014 11:36
Show Gist options
  • Save sahid/894c31f306bebacb2207 to your computer and use it in GitHub Desktop.
Save sahid/894c31f306bebacb2207 to your computer and use it in GitHub Desktop.
demo serial console
# demo os serial-console
#
# deps:
# easy_install ws4py
#
# nova.conf:
# [serial_console]
# enabled = true
#
# nova boot --image cirros --flavor 1 i1
# nova get-serial-console i1
# python client.py ws://127.0.0.1:6083/?token=5ed671c...
import sys
from ws4py.client.threadedclient import WebSocketClient
class LazyClient(WebSocketClient):
def run(self):
try:
while not self.terminated:
try:
b = self.sock.recv(4096)
sys.stdout.write(b)
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"
exit(1)
try:
ws = LazyClient(sys.argv[1], protocols=['binary'])
ws.connect()
while True:
# keyboard event...
c = sys.stdin.read(1)
if c:
ws.send(c)
ws.run_forever()
except KeyboardInterrupt:
ws.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment