Ejemplo n.º 1
0
def read_line_print(sock: socket):
    buf = bytearray(1024)
    while True:
        numbytes = sock.recv_into(buf, 1024)
        buf_str = buf[:numbytes].decode()
        print(buf_str, end='')
        if buf_str[-1] == chr(3):
            print()
            break
        elif buf_str[-1] == '\n':
            break
Ejemplo n.º 2
0
def read_line_to_str(sock: socket):
    buf = bytearray(1024)
    line = ""
    while True:
        numbytes = sock.recv_into(buf, 1024)
        buf_str = buf[:numbytes].decode()
        line = line + buf_str
        if buf_str[-1] == chr(3):
            break
        elif buf_str[-1] == '\n':
            break
    return line
Ejemplo n.º 3
0
def receive_set(sock: socket):
    num_elements_bytes = sock.recv(INT_SIZE)
    num_elements = struct.unpack('i', num_elements_bytes)[0]
    total_size = num_elements * INT_SIZE
    byte_buf = bytearray(total_size)
    view = memoryview(byte_buf)
    while total_size:
        received = sock.recv_into(view, total_size)
        view = view[received:]
        total_size -= received
    element_it = struct.iter_unpack("i", byte_buf)

    rel = set()
    for el in element_it:
        rel.add(el[0])
    byte_buf = None
    return rel
Ejemplo n.º 4
0
def receive_rel(sock: socket, num_cols: int):
    num_elements_bytes = sock.recv(INT_SIZE)
    num_elements = struct.unpack('i', num_elements_bytes)[0]
    total_size = num_elements * INT_SIZE
    byte_buf = bytearray(total_size)
    view = memoryview(byte_buf)
    while total_size:
        received = sock.recv_into(view, total_size)
        view = view[received:]
        total_size -= received
    element_it = struct.iter_unpack("i", byte_buf)

    rel = []
    col_idx = 0
    row = []
    for el in element_it:
        row.append(el[0])
        col_idx += 1
        if col_idx >= num_cols:
            rel.append(row)
            col_idx = 0
            row = []
    byte_buf = None
    return rel