-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsharkd_cli.py
135 lines (108 loc) · 3.02 KB
/
sharkd_cli.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/python3
# Copyright (C) 2016 Jakub Zawadzki
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import sys
import socket
import json
import threading
class SharkdClient:
def __init__(self, host, port):
self.mutex = threading.Lock()
self.buf = None
self.fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.fd.connect((host, port))
def __init__(self, path):
self.mutex = threading.Lock()
self.buf = None
self.fd = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
if path[0] == '@': # unix abstract socket
self.fd.connect('\0' + path[1:])
else:
self.fd.connect(path)
def _send_raw(self, data):
total = 0
while total < len(data):
sent = self.fd.send(data[total:])
if sent == 0:
raise RuntimeError("socket connection broken")
total += sent
def _send_str(self, s):
self._send_raw(s.encode())
def _recv_bytes(self, dest):
if self.buf == None:
self.buf = self.fd.recv(8192)
self.bufpos = 0
if len(self.buf) == 0:
self.buf = None
return None
start = self.bufpos
if isinstance(self.buf[0], int): # python3
pos = self.buf.find(10, start)
else: # python2
pos = self.buf.find('\n', start)
if pos != -1:
chunk = self.buf[start:pos]
nl = True
pos = pos + 1
else:
chunk = self.buf[start:]
nl = False
pos = len(self.buf)
dest.extend(chunk)
self.bufpos = pos
if len(self.buf) == self.bufpos:
self.buf = None
return nl
def _recv_line(self):
chunks = []
while True:
nl = self._recv_bytes(chunks)
if nl == None:
break
if nl == True:
break
if len(chunks) and isinstance(chunks[0], int): # python3
return bytes(chunks)
return b''.join(chunks)
def send(self, d):
js = json.dumps(d)
self._send_str(js + "\n")
def recv(self):
return self._recv_line().decode('utf8')
# send request, return every line
def send_req_gen(self, d):
try:
self.mutex.acquire()
self.send(d)
while True:
s = self.recv()
if len(s) == 0:
break
yield s
finally:
self.mutex.release()
# send request, return last line
def send_req(self, d):
last_line = ""
for last_line in self.send_req_gen(d):
pass
return last_line
if __name__ == '__main__':
filename = sys.argv[1]
cli = SharkdClient('@sharkd-socket')
for l in cli.send_req_gen(dict(req='load', file=filename)):
print("Loading: " + l)
cli.send_req(dict(req='bye'))