[Python] Understanding an instance of Python's struct.unpack

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I found sample code for interrogating NTP servers on https://www.mattcrampton.com/blog/query_an_ntp_server_from_python/. The code is brief and well-written, but I don't understand the use of struct.unpack. This is the core code:

    client = socket.socket(AF_INET,SOCK_DGRAM)
    client.sendto(msg.encode('utf-8'),address)
    msg,address = client.recvfrom(buf)

    t = struct.unpack("!12I",msg)[10]


    It returns an integer value (seconds from 1900-01-01) but I have two questions:

    • How does the unpack work in this instance? How did he arrive at "!12I" to do the decoding?
    • Is it possible to get a floating point value from the remote server?

    Continue reading...

Compartilhe esta Página