開始使用 Pyhton 之前,首先要確定電腦上能不能執行 Pyhton 程式。本教學將需要 Python 3.4 或更高的版本,因此請確認電腦上是否有對應版本。若不確定電腦上是否已經安裝 Python,建議直接按照以下步驟設定,以保證教學中的程式皆能夠正常執行。由於各作業系統安裝流程不同,以下請按照作業系統,選擇自己需要的部份閱讀。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import socket,sys,time | |
#时间服务器 | |
host = "stdtime.gov.hk" | |
#端口 | |
port = 37 | |
#时区 | |
curtz = 8 | |
#连接服务器,并接收返回 | |
try: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from socket import * | |
import datetime,os,struct,time,sys | |
# Script to set Linux hardware clock (/usr/sbin/hwclock) from an NTP | |
# time server. Run as "setclock.py" to simply print the time from | |
# the NTP server. Run as "setclock.py --set" to set the Linux | |
# hardware clock (as the super user, of course). | |
# Based on Simon Foster's simple SNTP client from ASPN Python cookbook. | |
# Adapted by Paul Rubin; this script lives at: | |
# http://www.nightsong.com/phr/python/setclock.py |