[Python network programming]1.13 SNTP Client

Source: Internet
Author: User

The purpose of blogging is to simply keep track of the problems that you have encountered while learning the Python web programming book, as well as the changes that the book code cannot run in python3.5.
I hope other friends who study this book will be able to save valuable time through my blog. Because just started to learn python, and the network is not very understanding of things, there are many things without explanation, will be supplemented.

TIME1970 = 2208988800l# is January 1, 1900 00:00:00~1970 year January 1 00:00:00 total number of seconds
Syntaxerror:invalid syntax
In Python 3, there is only one integer type int, expressed as a long integer, without a long in python2.

Def sntp_client ():
    client = Socket.socket (socket.af_inet, socket. SOCK_DGRAM)
    # Client main construction of the first three fields can be, after all the bits are filled 0
    # ' \x1b ' expanded to 00 100 011 respectively filled Li, VN, mode field
    # The remaining 47 * 8 = 376 bits are filled with 0
    D ATA = ' \x1b ' + * '
    #转化为字节形式
    client.sendto (Data.encode (), (Ntp_server, 123))
    data, address = Client.recv From (1024)
    if data:
        print (' Response received from: ', address)
    #按照给定的格式 (FMT) resolves a byte stream string that returns the parsed tuple
    t = struct.unpack ('!12i ', data) [ten]
    t-= TIME1970
    #ctime Converts the date and time to the string
    print (' \ttime=%s '% Time.ctime (t))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.