forked from tuna/tunasync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunasynctl.py
executable file
·41 lines (33 loc) · 1016 Bytes
/
tunasynctl.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
#!/usr/bin/env python2
# -*- coding:utf-8 -*-
import sys
import socket
import argparse
import json
import struct
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="tunasynctl")
parser.add_argument("-s", "--socket",
default="/var/run/tunasync.sock", help="socket file")
parser.add_argument("command", help="command")
parser.add_argument("target", nargs="?", default="__ALL__", help="target")
args = parser.parse_args()
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
sock.connect(args.socket)
except socket.error as msg:
print(msg)
sys.exit(1)
pack = json.dumps({
'cmd': args.command,
'target': args.target,
})
try:
sock.sendall(struct.pack('!H', len(pack)) + pack)
length = struct.unpack('!H', sock.recv(2))[0]
print(sock.recv(length))
except Exception as e:
print(e)
finally:
sock.close()
# vim: ts=4 sw=4 sts=4 expandtab