-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.py
More file actions
34 lines (27 loc) · 880 Bytes
/
connection.py
File metadata and controls
34 lines (27 loc) · 880 Bytes
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
import logging
logging.basicConfig(level=logging.INFO, format='[%(asctime)s] %(message)s')
class Connection:
"""
Helper class to manage and handle connection related operations
"""
def __init__(self, conn):
self.__EOT = '\n\x04\n'
self.__conn = conn
def get(self):
return self.__conn
def getpeername(self):
return self.__conn.getpeername()
def send(self, message):
self.__conn.send(f'{message}{self.__EOT}'.encode())
def recv(self):
data = ''
limit = 32000
while True:
limit -= 1
data += self.__conn.recv(1).decode()
if self.__EOT in data or limit == 0:
data = data.split(self.__EOT)[0]
break
if data:
logging.info(f'got data from {self.__conn.getpeername()}: {data}')
return data