-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatus_socket.py
105 lines (65 loc) · 2.32 KB
/
status_socket.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
""""""
import logging
import socket
import ssl
from config import *
from auth import Auth
from machine import Machine
from request_formatting import RequestFormatting
class StatusSocket(Auth, RequestFormatting, Machine):
def __init__(self, host, port, ssl = True):
self.host = host
self.port = port
self.ssl = ssl
def create_socket(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#self.create_socket(socket.AF_INET6, socket.SOCK_STREAM) #IPv6
#serv.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
def connect(self):
try:
self.socket.connect( (self.host, self.port) )
except:
return False
if self.ssl:
try:
self.socket = ssl.wrap_socket(self.socket, certfile=self.ssl_path(STATUS_CERTFILE), keyfile=self.ssl_path(STATUS_KEYFILE))
except:
return False
return True
def close_socket(self):
if hasattr(self, 'socket'):
self.socket.close()
def send(self, data):
if not self.ssl:
data = self.str_encode(data, 'StorADE Beta')
return self.socket.send(data)
def read(self):
recv = self.socket.recv(BUFSIZE)
cms, auth, size, data = self.parse_request(recv)
if data.get('success') and data.get('auth'):
self.save_auth(data['auth'])
def write(self):
data = self.make_request('STAT', self.full_info())
self.send(data)
def read_log(self):
return self.socket.recv(BUFSIZE)
def write_log(self):
message = ";;;\n".join(self.get_last_log(20))
data = self.make_request('STAT', message)
self.send(data)
def send_status_info(self):
#write to log when doing this
self.create_socket()
if self.connect():
logging.info("Sending status information")
self.write()
self.read()
self.close_socket()
def send_error(self):
r = False
self.create_socket()
if self.connect():
self.write_log()
r = self.read_log()
self.close_socket()
return r