forked from noyainrain/wall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sjmpc.py
executable file
·57 lines (48 loc) · 1.71 KB
/
sjmpc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python2
# Wall
# Python forward compatibility
from __future__ import (division, absolute_import, print_function,
unicode_literals)
import sys
import json
from argparse import ArgumentParser
import websocket
from websocket import WebSocketException
from wall import Message
class SjmpClient():
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))
def namedarg(value):
tokens = value.split('=', 1)
if len(tokens) != 2 or not tokens[0]:
raise ValueError('value_bad_format')
# TODO: convert value to specific type (string, number, true, false, null)
return tuple(tokens)
if __name__ == '__main__':
SjmpClient().run()