python脚本设置系统时间的两种方法
本文为大家分享了两种python脚本设置系统时间的方法,供大家参考,具体内容如下
第一种方法,使用Python设置系统时间,即给系统校时
#电脑时间用了一段时间后,系统时间不准了,想更新一下 #在windows里面,更新系统时间,时常失败,而且速度很忙. #在网上拷贝的代码,发现很好用,比windows自带的实现要快. #-*- coding:utf-8 -*- import socket import struct import time import win32api TimeServer = '210.72.145.44' #国家授时中心ip Port = 123 def getTime(): TIME_1970 = 2208988800L client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) data = '\x1b' + 47 * '\0' client.sendto(data, (TimeServer, Port)) data, address = client.recvfrom(1024) data_result = struct.unpack('!12I', data)[10] data_result -= TIME_1970 return data_result def setSystemTime(): tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst = time.gmtime(getTime()) win32api.SetSystemTime(tm_year, tm_mon, tm_wday, tm_mday, tm_hour, tm_min, tm_sec, 0) print "Set System OK!" if __name__ == '__main__': setSystemTime() print "%d-%d-%d %d:%d:%d" % time.localtime(getTime())[:6]
速度很快的,只要双击一下Py文件就可以了
第二种方法,python第三方库推荐,通过ntplib在windows上同步时间
很多时候我们有通过程序脚本同步校正北京时间的需求。
在linux上同步时间比较方便,安装个ntpdate软件就行了。
但是在windows的要同步时间比较麻烦。
这时想到的就是从网络获取一个准确的时间,然后调用dos命令修改时间。
从哪里获取呢?当然是国家授时中心。
授时中心的网址是 cn.pool.ntp.org(注意,流传甚广的210.72.145.44这个ip已经失效了,直接用域名。)
不过从授时中心获取的时间需要ntp协议解析。
ntplib就是干这事的。
另外值得一提的是在dos修改日期时间要通过2个命令实现,date命令修改日期,time命令修改时间。
安装ntplib:
easy_install ntplib或pip install ntplib
下面上代码。
import os import time import ntplib c = ntplib.NTPClient() response = c.request('pool.ntp.org') ts = response.tx_time _date = time.strftime('%Y-%m-%d',time.localtime(ts)) _time = time.strftime('%X',time.localtime(ts)) os.system('date {} && time {}'.format(_date,_time))
以上就是本文的全部内容,两种python脚本设置系统时间的方法,大家学会了吗?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
