-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
42 lines (31 loc) · 1.17 KB
/
server.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
import asyncio
import aiofiles
from datetime import datetime
import logging
import socket
from chat_connection import set_keepalive_linux, ChatConnection
from config import get_server_config
logger = logging.getLogger(__file__)
async def read_chat(host, port, path):
while True:
try:
async with ChatConnection(host, port) as (reader, writer):
data = await reader.readline()
message_time = datetime.now().strftime('[%d.%m.%y %H:%M]')
print(f'{message_time} {data.decode()}', end='')
async with aiofiles.open(path, 'a') as file:
await file.write(f'{message_time} {data.decode()}')
except socket.gaierror as e:
logging.exception(e)
async def main():
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
config = get_server_config()
if config:
await read_chat(config.host, config.port_out, config.path)
if __name__ == '__main__':
asyncio.run(main())