python修改操作系统时间的方法

yipeiwu_com6年前Python基础

本文实例讲述了python修改操作系统时间的方法。分享给大家供大家参考。具体实现方法如下:

#-*- 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]

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python的函数嵌套的使用方法

例子:复制代码 代码如下:def re_escape(fn):    def arg_escaped(this, *args):  &n...

在Python中用get()方法获取字典键值的教程

 get()方法返回给定键的值。如果键不可用,则返回默认值None。 语法 以下是get()方法的语法: dict.get(key, default=None) 参数...

Python RuntimeError: thread.__init__() not called解决方法

在写一个多线程类的时候调用报错 RuntimeError: thread.__init__() not called 复制代码 代码如下: class NotifyTread(thre...

Pycharm导入Python包,模块的图文教程

Pycharm导入Python包,模块的图文教程

1、点击File->settings 2、选择Project Interpreter,点击右边绿色的加号添加包 3、输入你想添加的包名,点击Install Package 4...

pytorch 自定义卷积核进行卷积操作方式

pytorch 自定义卷积核进行卷积操作方式

一 卷积操作:在pytorch搭建起网络时,大家通常都使用已有的框架进行训练,在网络中使用最多就是卷积操作,最熟悉不过的就是 torch.nn.Conv2d(in_channels,...