Completed
Push — master ( b34b71...1c0f48 )
by Andrii
12:24
created

Client.get_greeting()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
1
import socket
2
3
from heppy import Net
4
5
class Client:
6
    def __init__(self, address):
7
        self.socket  = None
8
        self.address = address
9
10
    def _connect(self):
11
        self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
12
        self.socket.settimeout(0.01)
13
        self.socket.connect(self.address)
14
        self.socket.settimeout(None)
15
16
    def connect(self):
17
        if self.socket is None:
18
            self._connect()
19
20
    def disconnect(self):
21
        self.socket = None
22
23
    def write(self, data):
24
        self.connect()
25
        Net.write(self.socket, data)
26
27
    def read(self):
28
        res = Net.read(self.socket)
29
        self.disconnect()
30
        return res
31
32
    def request(self, data):
33
        self.write(data)
34
        return self.read()
35
36
    def get_greeting(self):
37
        return self.request('greeting')
38
39
    @staticmethod
40
    def try_connect(address):
41
        try:
42
            client = Client(address)
43
            client.connect()
44
            return True
45
        except:
46
            return False
47
48