-
Notifications
You must be signed in to change notification settings - Fork 2
/
chat.py
47 lines (37 loc) · 1.3 KB
/
chat.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
import requests
import json
import argparse
import os
# the Windows console crashes when writing Unicode characters, this
# module is a fix for that
if os.name == 'nt':
import win_unicode_console
def get_reply(host, uuid, message, raw):
params = {'message': message, 'uuid': uuid}
r = requests.get(host, params=params)
if r.status_code == 200:
if raw:
return r.text
else:
return json.loads(r.text)['message'].replace('\\n', '\n')
else:
return 'Error: %s' % r.status_code
def make_url(host):
if not str.startswith(host, 'http://'):
host = 'http://' + host
if not str.endswith(host, '/'):
host += '/'
return host + 'api/response'
def main():
if os.name == 'nt':
win_unicode_console.enable()
parser = argparse.ArgumentParser()
parser.add_argument('host', help='address of the chat server (for example localhost:12345)')
parser.add_argument('--uuid', required=True, nargs=1, help='UUID of the object to talk with')
parser.add_argument('--raw', action='store_true', help='print raw JSON responses instead of replies')
args = parser.parse_args()
while True:
s = input('> ').strip()
print(get_reply(make_url(args.host), args.uuid, s, args.raw))
if __name__ == '__main__':
main()