flashstream-host-volumes-client 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flashstream_host_volumes_client/__init__.py +4 -0
- flashstream_host_volumes_client/client.py +174 -0
- flashstream_host_volumes_client-0.1.0.dist-info/METADATA +6 -0
- flashstream_host_volumes_client-0.1.0.dist-info/RECORD +6 -0
- flashstream_host_volumes_client-0.1.0.dist-info/WHEEL +5 -0
- flashstream_host_volumes_client-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import json
|
|
3
|
+
import math
|
|
4
|
+
import shlex
|
|
5
|
+
import socket
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class FlashstreamHostVolumesClient:
|
|
9
|
+
"""TCP client for Flashstream Host Volumes Server."""
|
|
10
|
+
|
|
11
|
+
MAX_RESPONSE_SIZE = 1024 * 1024
|
|
12
|
+
|
|
13
|
+
def __init__(
|
|
14
|
+
self,
|
|
15
|
+
host='127.0.0.1',
|
|
16
|
+
port=8765,
|
|
17
|
+
token='my-very-secret-token',
|
|
18
|
+
timeout=10,
|
|
19
|
+
):
|
|
20
|
+
if not isinstance(host, str) or not host or host != host.strip():
|
|
21
|
+
raise ValueError('Invalid host')
|
|
22
|
+
|
|
23
|
+
if isinstance(port, bool) or not isinstance(port, int) or not 1 <= port <= 65535:
|
|
24
|
+
raise ValueError('Invalid port')
|
|
25
|
+
|
|
26
|
+
if not isinstance(token, str):
|
|
27
|
+
raise ValueError('Token must be a string')
|
|
28
|
+
|
|
29
|
+
if any(ord(c) < 32 or ord(c) == 127 for c in token):
|
|
30
|
+
raise ValueError('Invalid token')
|
|
31
|
+
|
|
32
|
+
if (
|
|
33
|
+
isinstance(timeout, bool)
|
|
34
|
+
or not isinstance(timeout, (int, float))
|
|
35
|
+
or not math.isfinite(timeout)
|
|
36
|
+
or timeout <= 0
|
|
37
|
+
):
|
|
38
|
+
raise ValueError('Invalid timeout')
|
|
39
|
+
|
|
40
|
+
self.host = host
|
|
41
|
+
self.port = port
|
|
42
|
+
self.token = token
|
|
43
|
+
self.timeout = timeout
|
|
44
|
+
|
|
45
|
+
self.last_error = None
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def request(self, command, **params):
|
|
49
|
+
self.last_error = None
|
|
50
|
+
|
|
51
|
+
action = command.upper() if isinstance(command, str) else ''
|
|
52
|
+
|
|
53
|
+
try:
|
|
54
|
+
if not isinstance(command, str) or not command.isascii() or not command.isalpha():
|
|
55
|
+
raise ValueError('Invalid command')
|
|
56
|
+
|
|
57
|
+
if 'token' in params:
|
|
58
|
+
raise ValueError('Token is managed by the client')
|
|
59
|
+
|
|
60
|
+
fields = {**params}
|
|
61
|
+
parts = [action]
|
|
62
|
+
|
|
63
|
+
for key, value in fields.items():
|
|
64
|
+
if not key.isascii() or not key.isidentifier():
|
|
65
|
+
raise ValueError(f'Invalid parameter: {key}')
|
|
66
|
+
|
|
67
|
+
value = str(value)
|
|
68
|
+
|
|
69
|
+
if any(ord(c) < 32 or ord(c) == 127 for c in value):
|
|
70
|
+
raise ValueError(f'Invalid value: {key}')
|
|
71
|
+
|
|
72
|
+
parts.append(f'{key}={shlex.quote(value)}')
|
|
73
|
+
|
|
74
|
+
token_hashed = hashlib.sha256(self.token.encode('utf-8')).hexdigest()
|
|
75
|
+
parts.append(f'token={shlex.quote(token_hashed)}')
|
|
76
|
+
|
|
77
|
+
request = ' '.join(parts) + '\n'
|
|
78
|
+
|
|
79
|
+
with socket.create_connection(
|
|
80
|
+
(self.host, self.port),
|
|
81
|
+
timeout=self.timeout,
|
|
82
|
+
) as sock:
|
|
83
|
+
sock.settimeout(self.timeout)
|
|
84
|
+
|
|
85
|
+
with sock.makefile('rwb') as stream:
|
|
86
|
+
stream.write(request.encode('utf-8'))
|
|
87
|
+
stream.flush()
|
|
88
|
+
|
|
89
|
+
raw = stream.readline(self.MAX_RESPONSE_SIZE + 1)
|
|
90
|
+
|
|
91
|
+
if len(raw) > self.MAX_RESPONSE_SIZE:
|
|
92
|
+
raise ValueError('Server response is too large')
|
|
93
|
+
|
|
94
|
+
if not raw or not raw.endswith(b'\n'):
|
|
95
|
+
raise ValueError('Incomplete server response')
|
|
96
|
+
|
|
97
|
+
line = raw.decode('utf-8').removesuffix('\n').removesuffix('\r')
|
|
98
|
+
response = self._parse_response(line)
|
|
99
|
+
|
|
100
|
+
if response['status'] == 'error':
|
|
101
|
+
self.last_error = response.get('message', 'Server error')
|
|
102
|
+
|
|
103
|
+
return response
|
|
104
|
+
|
|
105
|
+
except (OSError, ValueError, UnicodeError) as exc:
|
|
106
|
+
return self._error(action, str(exc))
|
|
107
|
+
|
|
108
|
+
@staticmethod
|
|
109
|
+
def _parse_response(line):
|
|
110
|
+
parts = shlex.split(line)
|
|
111
|
+
|
|
112
|
+
if not parts or not parts[0].startswith('status='):
|
|
113
|
+
raise ValueError('Invalid server response')
|
|
114
|
+
|
|
115
|
+
response = {}
|
|
116
|
+
|
|
117
|
+
for part in parts:
|
|
118
|
+
key, separator, value = part.partition('=')
|
|
119
|
+
|
|
120
|
+
if (
|
|
121
|
+
not separator
|
|
122
|
+
or not key.isascii()
|
|
123
|
+
or not key.isidentifier()
|
|
124
|
+
or key in response
|
|
125
|
+
):
|
|
126
|
+
raise ValueError('Invalid response field')
|
|
127
|
+
|
|
128
|
+
response[key] = value
|
|
129
|
+
|
|
130
|
+
if response['status'] not in ('ok', 'error'):
|
|
131
|
+
raise ValueError('Invalid response status')
|
|
132
|
+
|
|
133
|
+
return response
|
|
134
|
+
|
|
135
|
+
def _error(self, action, message):
|
|
136
|
+
self.last_error = message
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
'status': 'error',
|
|
140
|
+
'action': action,
|
|
141
|
+
'message': message,
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
def ping(self):
|
|
145
|
+
return self.request('PING')
|
|
146
|
+
|
|
147
|
+
def get_status(self):
|
|
148
|
+
return self.request('STATUS')
|
|
149
|
+
|
|
150
|
+
def get_volumes(self):
|
|
151
|
+
response = self.request('VOLUMES')
|
|
152
|
+
|
|
153
|
+
if response.get('status') != 'ok':
|
|
154
|
+
return []
|
|
155
|
+
|
|
156
|
+
try:
|
|
157
|
+
volumes = json.loads(response['volumes'])
|
|
158
|
+
|
|
159
|
+
if isinstance(volumes, list) and all(
|
|
160
|
+
isinstance(volume, dict) for volume in volumes
|
|
161
|
+
):
|
|
162
|
+
return volumes
|
|
163
|
+
|
|
164
|
+
except (KeyError, TypeError, ValueError):
|
|
165
|
+
pass
|
|
166
|
+
|
|
167
|
+
self.last_error = 'Invalid volumes response'
|
|
168
|
+
return []
|
|
169
|
+
|
|
170
|
+
def eject(self, path):
|
|
171
|
+
if not path:
|
|
172
|
+
return self._error('EJECT', 'Volume path cannot be empty')
|
|
173
|
+
|
|
174
|
+
return self.request('EJECT', path=path)
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
flashstream_host_volumes_client/__init__.py,sha256=_cmS23m4hn1c6CPH1LUGPIsc_LS0d4PHhgHeMMnpDmc,93
|
|
2
|
+
flashstream_host_volumes_client/client.py,sha256=cLYr7Il0oliJQZAcDKZgZSZ3eB-ueh1e6kLacrlRtJs,4976
|
|
3
|
+
flashstream_host_volumes_client-0.1.0.dist-info/METADATA,sha256=xmykjXfc5eS74X-PZH0Ts8LNRwchGqZtL9brRGzdqE0,179
|
|
4
|
+
flashstream_host_volumes_client-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
5
|
+
flashstream_host_volumes_client-0.1.0.dist-info/top_level.txt,sha256=lqiVraPouvlLJicX-CcCZO10BDYy5_xKN_W-XCN9teA,32
|
|
6
|
+
flashstream_host_volumes_client-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
flashstream_host_volumes_client
|