class Client object Client class which will be connected to server def

 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
class Client(object):
"""
Client class which will be connected to server
"""
def __init__(self, jid, password):
self._socket = None
def connect(self, host='127.0.0.1', port=5222):
"""
Connect to server
"""
for res in socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM):
family, socktype, proto, canonname, sockaddr = res
try:
self._socket = socket.socket(family, socktype, proto)
except socket.error, msg:
self._socket = None
continue
try:
self._socket.connect(sockaddr)
except socket.error, message:
self._socket.close()
self._socket = None
continue
break
if self._socket is None:
raise ConnectionError("Could'not connect to server")
def disconnect(self):
"""
Disconnecting from server
"""
if self._socket:
self._socket.close()
self._socket = None
def send(self, data):
if self._socket:
self._socket.send(data)
else:
raise ConnectionError("Not connected")
def receive(self):
if self._socket:
data = ""
while True:
received = self._socket.recv(1024)
if received:
data += received
else:
break
return data
else:
raise ConnectionError("Not connected")