import curses import os from six.moves import input import sys from threading import Thread curses.setupterm(os.environ['TERM']) def clear_terminal(): f = sys.stdout try: f = f.buffer except AttributeError: pass f.write(curses.tigetstr('clear')) f.flush() class ConsoleServerBrowser(object): def __init__(self, client): self.client = client self.result = None self.services = [] def read_input(self): while self.result is None: line = input() try: self.result = int(line) except ValueError: pass self.client.mainloop.quit() def update(self, services): self.services = services # print the new output output = '\n'.join( '{0}: {name} @ {host} ({address}:{port})'.format(i, **service) for i, service in enumerate(services, 1) ) clear_terminal() print(output) def run(self, client_run): t = Thread(target=self.read_input) t.setDaemon(True) t.start() client_run() service = self.services[self.result - 1] return service['target']