VOOZH about

URL: https://qiita.com/c-yan/items/90209045909e1f1c2ffb

⇱ Python で ntp server から時刻を取得する (Python 3 版) #Python - Qiita


👁 Image
3

Go to list of users who liked

2

Share on X(Twitter)

Share on Facebook

Add to Hatena Bookmark

More than 5 years have passed since last update.

Python で ntp server から時刻を取得する (Python 3 版)

3
Last updated at Posted at 2019-04-28

Python で ntp server から時刻を取得する (Python 3 版)

Python で ntp server から時刻を取得する (Python 2 版) を Python 3 に移植した.
2to3 を使ってみたら、2208988800L の最後の L を取れって言われたので、それを修正して実行してみたら

TypeError: a bytes-like object is required, not 'str'

って怒られた. しょんぼり.

テスト

>>> ntp_now('ntp.jst.mfeed.ad.jp')
datetime.datetime(2019, 4, 29, 0, 3, 30)

コード

def ntp_now(server, port = 123):
 from socket import socket, AF_INET, SOCK_DGRAM
 from struct import unpack
 from datetime import datetime
 with socket(AF_INET, SOCK_DGRAM) as s:
 s.sendto(b'\x1b' + 47 * b'\0', (server, port))
 result = s.recvfrom(1024)[0]
 if result:
 return datetime.fromtimestamp(unpack(b'!12I', result)[10] - 2208988800)
 else:
 None

b を付けまくって、finally close を with 文で差し替えた. with がデフォルトで使えるのは 2.6 からだから 2.5 時代に書いたのかなあ.

3

Go to list of users who liked

2
0

Go to list of comments

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3

Go to list of users who liked

2