-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.py
59 lines (43 loc) · 1.28 KB
/
connection.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
import socket
import re
from threading import Lock
from glob_const import *
import logging
# connection class
class connection:
def __init__(self, server_info):
self.logger = logging.getLogger(__name__)
self.logger.debug("Initialize")
self._server_info = server_info
self._server_addr = self._server_info['address']
try:
self._server_port = int(self._server_info['port'])
except ValueError:
self.logger.critical("Error in connection initialization")
return -1
self._lock = Lock()
self._sock = 0
def socket_recv(self):
self._lock.acquire()
try:
recv_data = self._sock.recv(4096)
while(recv_data.endswith(CONST_CRLF) != True):
recv_data = recv_data + self._sock.recv(4096)
self.logger.debug(recv_data)
# data_list = recv_data.split()
# self.logger.debug(data_list)
return recv_data
finally:
self._lock.release()
def socket_send(self, data):
data_length = len(data)
bytes_sent = 0
while bytes_sent < data_length:
data = data[bytes_sent:]
data_length = len(data)
bytes_sent = self._sock.send(data)
def connect_server(self):
self._sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
self._sock.connect( (self._server_addr, self._server_port) )
def disconnect_server(self):
self._sock.close()