summaryrefslogtreecommitdiff
path: root/day4/task3/server.py
blob: b3cb6198f306e009e09c13ed2021ead6ad0c5be7 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import socket
import struct
from db import search_by_date
import re
import json


def pack_data(data):
    encoded = data.encode()
    return struct.pack('I', len(encoded)) + encoded


def handle_request(data):
    match = re.match(r'(\d+):?(\d+)?:?(\d+)?-(.*)', data)
    if match:
        *date, msg = match.groups()
        return pack_data(json.dumps(search_by_date(*date, msg)))

    else:
        print('String not matched.')
        return pack_data(json.dumps([]))


def get_data(connection):
    buffer = b''
    while len(buffer) < 4:
        data = connection.recv(4 - len(buffer))
        if not data:
            raise ConnectionError('Connection was closed')

        buffer += data

    size_packed, buffer = buffer[:4], buffer[4:]
    if not size_packed:
        raise ConnectionError('Connection was closed')

    (size,) = struct.unpack('I', size_packed)
    data = connection.recv(size - len(buffer))
    return buffer + data


def handle_connection(connection, address):
    while True:
        try:
            data = get_data(connection)

        except ConnectionResetError:
            print(f'Connection with {address[0]} was unexpectedly closed')
            break

        except ConnectionError:
            print(f'Connection with {address[0]} was ended')
            break

        except KeyboardInterrupt:
            raise KeyboardInterrupt()

        else:
            if not data:
                print(f'Connection with {address[0]} was ended')
                break
            response = handle_request(data.decode())
            connection.sendall(response)


HOST, PORT = ADDR = '0.0.0.0', 6001
sock = socket.socket()
sock.bind(ADDR)
sock.listen(5)

while True:
    print('Listening for new connections...')
    try:
        connection, address = sock.accept()
        print(f'Got a connection from {address[0]}')
        handle_connection(connection, address)

    except KeyboardInterrupt:
        print('Stopping server...')
        sock.close()
        print('Server stopped')
        break