Exemplo n.º 1
0
def run_server(server_address):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind(server_address)
    logging.info("Serverul a pornit pe %s si portnul portul %d", server_address[0], server_address[1])
    
    while True:
        logging.info('Asteptam mesaje...')
        data, address = sock.recvfrom(4096)
        
        logging.info("Am primit %s octeti de la %s", len(data), address)
        logging.info('Content primit: "%s"', data)
        mesaj_binar = construieste_mesaj_raw(address[0], server_address[0], address[1], server_address[1], data)
        valoare_numerica = calculeaza_checksum(mesaj_binar)
        valoare = hex(valoare_numerica)
        logging.info('Checksum calculat: %s', str(valoare))
        sock.sendto(str(valoare), address)
Exemplo n.º 2
0
def send_message(address, message):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        logging.info('Trimitem mesajul "%s" catre %s:%d', message, address[0],
                     address[1])
        sock.sendto(message.encode('utf-8'), address)

        logging.info('Asteptam un raspuns...')
        data, server = sock.recvfrom(4096)
        logging.info('Content primit: "%s"', data)

        # Calculam checksum pentru pachetul primit
        ip, port = sock.getsockname()
        print ip
        data_raw = construieste_mesaj_raw(server[0], ip, server[1], port, data)
        checksum_value = calculeaza_checksum(data)
        checksum_value = hex(checksum_value)

        logging.info('Checksum-ul calculat pentru %s este %s', data,
                     str(checksum_value))
    finally:
        logging.info('closing socket')
        sock.close()
Exemplo n.º 3
0
def send_message(address, message):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    ip_actual = socket.gethostbyname(socket.gethostname())
    port = 20000
    address_act = (ip_actual, port)
    sock.bind(address_act)

    try:
        logging.info('Trimitem mesajul "%s" catre %s:%d', message, address[0],
                     address[1])
        sock.sendto(message.encode('utf-8'), address)

        logging.info('Asteptam un raspuns...')
        data, server = sock.recvfrom(4096)
        mesaj_binar = construieste_mesaj_raw(ip_actual, server[0], port,
                                             server[1], data)
        logging.info('Content primit: "%s"', data)
        logging.info('Checksum calculat: %s',
                     str(hex(calculeaza_checksum(mesaj_binar))))

    finally:
        logging.info('closing socket')
        sock.close()