Esempio n. 1
0
File: sjmpc.py Progetto: dnnr/wall
    def run(self):
        parser = ArgumentParser()
        parser.add_argument('url')
        parser.add_argument('type')
        parser.add_argument('arg', type=namedarg, nargs='*')
        args = parser.parse_args()

        try:
            connection = websocket.create_connection(args.url)
        except ValueError as e:
            parser.error(
                "argument url: invalid url value: '{}'".format(args.url))
        except (IOError, WebSocketException) as e:
            print('error: failed to connect (details: {})'.format(e),
                file=sys.stderr)
            sys.exit(1)

        try:
            call_msg = Message(args.type, dict(args.arg))
            connection.send(str(call_msg))
            while True:
                result_msg = Message.parse(connection.recv())
                if result_msg.type == call_msg.type:
                    break
            connection.close()
        except WebSocketException as e:
            print('error: disconnected (details: {})'.format(e),
                file=sys.stderr)
            sys.exit(1)

        print(json.dumps(result_msg.data, indent=4))
Esempio n. 2
0
 def _call(self, type, args):
     self._connection.send(str(Message(type, args)))
     while True:
         msg = Message.parse(self._connection.recv())
         if msg.type == type:
             return msg.data