search
HomeBackend DevelopmentPython TutorialPython语言编写电脑时间自动同步小工具

Python语言编写电脑时间自动同步小工具

Jun 06, 2016 am 11:27 AM
pythoncomputer timeAutomatic synchronization

话不多说,小工具需求如下:
功能需求 -- 电脑开机后自动执行时间同步
非功能需求 -- 安装执行简单,无需安装额外环境

一、代码实现

基于以上需求,思路如下:访问网络获取北京时间,然后调用命令行来设置系统时间。程序写成Windows Service,并设置为开机自动运行。正好前段时间在学习Python,所以打算用Python来写这个工具。具体代码如下:

获取网络时间

代码如下:


 def getBeijinTime():
     """
    获取北京时间
     """
     try:
         conn = httplib.HTTPConnection("www.beijing-time.org")
         conn.request("GET", "/time.asp")
         response = conn.getresponse()
         print response.status, response.reason
         if response.status == 200:
             #解析响应的消息
             result = response.read()
             logging.debug(result)
             data = result.split("\r\n")
             year = data[1][len("nyear")+1 : len(data[1])-1]
             month = data[2][len("nmonth")+1 : len(data[2])-1]
             day = data[3][len("nday")+1 : len(data[3])-1]
             #wday = data[4][len("nwday")+1 : len(data[4])-1]
             hrs = data[5][len("nhrs")+1 : len(data[5])-1]
             minute = data[6][len("nmin")+1 : len(data[6])-1]
             sec = data[7][len("nsec")+1 : len(data[7])-1]

             beijinTimeStr = "%s/%s/%s %s:%s:%s" % (year, month, day, hrs, minute, sec)
             beijinTime = time.strptime(beijinTimeStr, "%Y/%m/%d %X")
             return beijinTime
     except:
         logging.exception("getBeijinTime except")
         return None


同步本地系统时间

代码如下:


 def syncLocalTime():
     """
     同步本地时间
     """
     logging.info("current local time is: %d-%d-%d %d:%d:%d" % time.localtime()[:6])

     beijinTime = getBeijinTime()
     if beijinTime is None:
         logging.info("get beijinTime is None, will try again in 30 seconds...")
         timer = threading.Timer(30.0, syncLocalTime)
         timer.start();
     else:
         logging.info("get beijinTime is: %d-%d-%d %d:%d:%d" % beijinTime[:6])

         tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec = beijinTime[:6]
         import os
         os.system("date %d-%d-%d" % (tm_year, tm_mon, tm_mday))     #设置日期
         os.system("time %d:%d:%d.0" % (tm_hour, tm_min, tm_sec))    #设置时间
         logging.info("syncLocalTime complete, current local time: %d-%d-%d %d:%d:%d \n" % time.localtime()[:6])

二、部署安装

为了让Python程序能以Windows服务的方式运行,需要用到py2exe(用来把Python程序编译成exe)和Python Win32 Extensions 。(py2exe把Python代码编译成Winodws服务时依赖此组件)下载并安装这两个组件。安装完毕后,在Python的安装目录下找到py2exe的Windows Service示例({PythonRoot}\Lib\site-packages\py2exe\samples\advanced\MyService.py)。然后仿照这个示例将上面的代码完善一下。

Windows服务示例

代码如下:


 import win32serviceutil
 import win32service
 import win32event
 import win32evtlogutil

 class SynctimeService(win32serviceutil.ServiceFramework):
     _svc_name_ = "Synctime"
     _svc_display_name_ = "Synctime"
     _svc_description_ = "Synchronize local system time with beijin time"
    _svc_deps_ = ["EventLog"]

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        import servicemanager

        # Write a 'started' event to the event log...
        win32evtlogutil.ReportEvent(self._svc_name_,
                                    servicemanager.PYS_SERVICE_STARTED,
                                    0, # category
                                    servicemanager.EVENTLOG_INFORMATION_TYPE,
                                    (self._svc_name_, ''))

        # wait for beeing stopped...
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

        # and write a 'stopped' event to the event log.
        win32evtlogutil.ReportEvent(self._svc_name_,
                                    servicemanager.PYS_SERVICE_STOPPED,
                                    0, # category
                                    servicemanager.EVENTLOG_INFORMATION_TYPE,
                                    (self._svc_name_, ''))  

if __name__ == '__main__':
    # Note that this code will not be run in the 'frozen' exe-file!!!
    win32serviceutil.HandleCommandLine(SynctimeService) 



之后,再编写一个steup.py文件用来生成安装文件。

Setup.py

代码如下:


 from distutils.core import setup
 import py2exe

 setup(
     # The first three parameters are not required, if at least a
     # 'version' is given, then a versioninfo resource is built from
     # them and added to the executables.
     version = "0.0.1",
     description = "Synchroniz local system time with beijin time",
     name = "sysctime",

     # targets to build
     # console = ["synctime.py"],
     service=["synctime"]
 )

编译生成windows程序,如下图:

然后在控制台中运行:setup.py py2exe ,一切顺利的话会在当前目录下生成build和dist目录。

控制台目录切换到dist目录,找到synctime.exe,在命令行中运行:

synctime.exe –install (-remove)  安装或移除时间同步服务。

现在可以运行services.msc查看服务运行情况

可以看到服务并没有启动,而且启动方式为手动。在这里可以右击服务选择属性手动把服务启动起来,并且设置为服务自动启动。

好吧,我承认。这样操作跟上面的需求有点出入了,略显麻烦。为了解决这个问题,自然想到的是用批处理来做。在dist目录下分别建两个批处理文件:

installservice.bat

代码如下:


 @echo off

 :: 安装windows服务
 echo 正在安装服务,请稍候...
 synctime.exe -install

 :: 设置服务自动启动
 echo 正在启动服务...
 sc config Synctime start= AUTO

:: 启动服务
sc start Synctime

echo 服务启动成功, 按任意键继续...
pause



removeserivce.bat

代码如下:


@echo off

:: 停止服务
echo 正在停止服务,请稍候...
sc stop Synctime

echo 正在卸载服务...
:: 删除windows服务
synctime.exe -remove

echo 服务卸载完成,请按任意键继续剩余卸载...
pause

好了,现在可以把dist打个包发给老妈用了。但是,这样发个一个压缩包,看起来也太不专业了。解决的办法是打一个安装包,把bat脚本打到安装包里,在安装程序时由安装包调用。这里我用的是NISI(使用HM VNISEdit打包向导来生成打包脚本非常方便)。

三、最终安装效果图

四、结尾

遗留的问题:

1、从上面的截图可以看到,安装程序在调用批处理时会显示出控制台窗口。这个问题我在网上查找资料,NSIS有相关的插件可以隐藏控制台窗口调用bat文件。
2、我源代码中有写日志文件的操作,但是以Windows服务的方式运行后,日志文件不能写了,不知道有没有好的解决办法。
3、360 ...真是要人命啊....Orz..

时间同步工具及源代码:http://www.bitsCN.com/softs/74865.html

编译方法:

第一步: 安装Python环境(什么?还没有Python环境?... - -!)
第二步: 安装依赖组件
1、py2exe (目前只支持到python2.7)
2、Python Win32 Extensions

第三步(可选):安装Nsis环境,用来编译脚本

第四步:将synctime.py编译成windows程序
1、在当前目录下运行"setup.py py2exe",顺利的话会在当前目录下生成dist和build目录

第五步: 运行,有两种运行方式:
1、将installservice.bat 和 removeservice.bat 拷贝到dist中运行即可
2(依赖第三步)、使用Nsis编译Synctime.nsi脚本,生成安装包, 安装后运行(推荐)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python's Execution Model: Compiled, Interpreted, or Both?Python's Execution Model: Compiled, Interpreted, or Both?May 10, 2025 am 12:04 AM

Pythonisbothcompiledandinterpreted.WhenyourunaPythonscript,itisfirstcompiledintobytecode,whichisthenexecutedbythePythonVirtualMachine(PVM).Thishybridapproachallowsforplatform-independentcodebutcanbeslowerthannativemachinecodeexecution.

Is Python executed line by line?Is Python executed line by line?May 10, 2025 am 12:03 AM

Python is not strictly line-by-line execution, but is optimized and conditional execution based on the interpreter mechanism. The interpreter converts the code to bytecode, executed by the PVM, and may precompile constant expressions or optimize loops. Understanding these mechanisms helps optimize code and improve efficiency.

What are the alternatives to concatenate two lists in Python?What are the alternatives to concatenate two lists in Python?May 09, 2025 am 12:16 AM

There are many methods to connect two lists in Python: 1. Use operators, which are simple but inefficient in large lists; 2. Use extend method, which is efficient but will modify the original list; 3. Use the = operator, which is both efficient and readable; 4. Use itertools.chain function, which is memory efficient but requires additional import; 5. Use list parsing, which is elegant but may be too complex. The selection method should be based on the code context and requirements.

Python: Efficient Ways to Merge Two ListsPython: Efficient Ways to Merge Two ListsMay 09, 2025 am 12:15 AM

There are many ways to merge Python lists: 1. Use operators, which are simple but not memory efficient for large lists; 2. Use extend method, which is efficient but will modify the original list; 3. Use itertools.chain, which is suitable for large data sets; 4. Use * operator, merge small to medium-sized lists in one line of code; 5. Use numpy.concatenate, which is suitable for large data sets and scenarios with high performance requirements; 6. Use append method, which is suitable for small lists but is inefficient. When selecting a method, you need to consider the list size and application scenarios.

Compiled vs Interpreted Languages: pros and consCompiled vs Interpreted Languages: pros and consMay 09, 2025 am 12:06 AM

Compiledlanguagesofferspeedandsecurity,whileinterpretedlanguagesprovideeaseofuseandportability.1)CompiledlanguageslikeC arefasterandsecurebuthavelongerdevelopmentcyclesandplatformdependency.2)InterpretedlanguageslikePythonareeasiertouseandmoreportab

Python: For and While Loops, the most complete guidePython: For and While Loops, the most complete guideMay 09, 2025 am 12:05 AM

In Python, a for loop is used to traverse iterable objects, and a while loop is used to perform operations repeatedly when the condition is satisfied. 1) For loop example: traverse the list and print the elements. 2) While loop example: guess the number game until you guess it right. Mastering cycle principles and optimization techniques can improve code efficiency and reliability.

Python concatenate lists into a stringPython concatenate lists into a stringMay 09, 2025 am 12:02 AM

To concatenate a list into a string, using the join() method in Python is the best choice. 1) Use the join() method to concatenate the list elements into a string, such as ''.join(my_list). 2) For a list containing numbers, convert map(str, numbers) into a string before concatenating. 3) You can use generator expressions for complex formatting, such as ','.join(f'({fruit})'forfruitinfruits). 4) When processing mixed data types, use map(str, mixed_list) to ensure that all elements can be converted into strings. 5) For large lists, use ''.join(large_li

Python's Hybrid Approach: Compilation and Interpretation CombinedPython's Hybrid Approach: Compilation and Interpretation CombinedMay 08, 2025 am 12:16 AM

Pythonusesahybridapproach,combiningcompilationtobytecodeandinterpretation.1)Codeiscompiledtoplatform-independentbytecode.2)BytecodeisinterpretedbythePythonVirtualMachine,enhancingefficiencyandportability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version