forked from albertz/music-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketcontrol-interactiveclient.py
76 lines (62 loc) · 1.74 KB
/
socketcontrol-interactiveclient.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
# MusicPlayer, https://github.com/albertz/music-player
# Copyright (c) 2013, Albert Zeyer, www.az2000.de
# All rights reserved.
# This code is under the 2-clause BSD license, see License.txt in the root directory of this project.
import better_exchook
better_exchook.install()
import binstruct
import socket
import appinfo
import tempfile
from glob import glob
s = socket.socket(socket.AF_UNIX)
import os,sys
if len(sys.argv) > 1:
sockfile = sys.argv[1]
assert os.path.exists(sockfile)
s.connect(sockfile)
else:
tmpdir = tempfile.gettempdir()
files = glob("%s/%s-*-socketcontrol" % (tmpdir, appinfo.appid))
assert files
for fn in files:
sockfile = fn
try:
s.connect(sockfile)
except socket.error:
pass
else:
print "socket:", sockfile
break
assert s
s.setblocking(True)
f = s.makefile()
serverappid,servername,serverver = binstruct.read(f)
print "connected to", serverappid, servername, serverver
assert serverappid == appinfo.appid
assert serverver == 0.1
binstruct.write(f, (appinfo.appid, "SocketControl-InteractiveClient", 0.1, "ok"))
f.flush()
try: import readline
except ImportError: pass # ignore
idx = 0
while True:
try: s = raw_input("> ")
except (KeyboardInterrupt,EOFError):
print("")
sys.exit(0)
if s.strip() == "": continue
f.write(binstruct.varEncode((idx, s)).tostring())
f.flush()
answeridx,answertype,answerret = binstruct.varDecode(f)
assert answeridx == idx
if answertype == "compile-exception":
print("%s : %s in %r" % (answerret[0], answerret[1], s))
elif answertype == "eval-exception":
print("Exception %s : %s" % (answerret[0], answerret[1]))
elif answertype == "return":
if answerret is not None:
print(answerret)
else:
assert False, "%s unknown" % answertype