gpt4 book ai didi

Python 更新系统时间

转载 作者:太空宇宙 更新时间:2023-11-03 20:22:37 31 4
gpt4 key购买 nike

我有一个对远程服务器上的时间敏感的应用程序 - 它需要在特定时刻运行。连续循环和检索服务器时间并不能让我达到我想要的程度。我正在尝试检索服务器时间并提前更新系统时间(Python 3.7,Windows 10),以便模块可以尽可能接近服务器时间重新启动。我在win32api中找到了SetSysemTime函数。

这是我创建的用于测试各种方法的两行模块:

import win32api
win32api.SetSystemTime(2020,9,1,21,9,10,10,0)

当我运行此程序时,出现以下错误:

Traceback (most recent call last): File "C:\Users\pinev\AppData\Local\Programs\Python\Python37\Projects\Threading Tests\settime.py", line 4, in win32api.SetSystemTime(2020,9,1,21,9,10,10,0) pywintypes.error: (1314, 'SetSystemTime', 'A required privilege is not held by the client.')

所以看来我有正确的解决方案,但不知何故需要更改模块的权限才能让 Win10 处理请求。我研究了在 Python 模块中设置权限的方法,但似乎找不到任何有效的方法。如果有人可以提供解决方案或引用,我们将不胜感激。

提前致谢。

最佳答案

如果您只需要提升权限来运行Python程序,您可以以管理员身份启动该程序。

这也是我认为最简单、最直接的方法。

测试代码:

import win32api
import ctypes, sys

def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False

if is_admin():
win32api.SetSystemTime(2020,9,1,21,9,10,10,0)
else:
# Re-run the program with admin rights
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)

关于Python 更新系统时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58058796/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com