How to set the system time of the timeout mechanism in a python script

Source: Internet
Author: User
This article mainly introduces how to set the system time of the timeout mechanism for python scripts. If you are interested, refer to this article to introduce how to set the system time for python scripts, there are two types, One is to call the socket to directly send the udp packet to the National Time Service Center, and the other is to call the ntplib Packet. When I ping the cn.pool.ntp.org address of the National Time Service Center on a local computer, packet loss occurs sometimes. However, neither of them checks whether udp packet loss is enabled. method 1 is stuck after udp packet loss and cannot exit, method 2: Although timeout is prompted, no other attempts are made, such as resending a packet or sending a packet to different IP addresses of the same domain name. Therefore, I tried to add a timeout mechanism based on the Code of method 1 and try to send packets to different IP addresses of the same domain name.

The complete code after modification is as follows:

#-*- coding:utf-8 -*-import socketimport structimport timeimport win32apiimport osimport redef getTime(TimeServerAddresses):  TIME_1970 = 2208988800L  client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  client.settimeout(3)  data = '\x1b' + 47 * '\0'  #TimeServer_ip=socket.gethostbyname('cn.pool.ntp.org')  #TimeServer_ip='202.118.1.130'  Port=123  for address in TimeServerAddresses:    success=False    count=0    while not success and count<3:      print address,count      try:        client.sendto(data, (address, Port))        data, address = client.recvfrom(1024)        success=True      except socket.timeout:        print 'Request timed out!'        count=count+1    if success==True:a      break  data_result = struct.unpack('!12I', data)[10]  data_result -= TIME_1970  return data_resultdef setSystemTime(now_time):  tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst = time.gmtime(now_time)  win32api.SetSystemTime(tm_year, tm_mon, tm_wday, tm_mday, tm_hour, tm_min, tm_sec, 0)  print "Set System OK!"def getServerIP():  res1=os.popen('nslookup cn.pool.ntp.org')  result1=res1.read()  addresses=result1.split('\n\n')[1].split('\n')[1][12:].split(',')  return addresses  #for address in addresses:  #  res=os.popen('ping -n 2 '+address)  #  result=res.read()  #  received_num=int(re.findall('Received = [0-9]',result)[0].split('=')[1])  #  if received_num > 1:  #    break  #TimeServer=addressif __name__ == '__main__':  addresses=getServerIP()  now_time=getTime(addresses)  setSystemTime(now_time)  print "%d-%d-%d %d:%d:%d" % time.localtime(now_time)[:6]

The above is all the content of this article, hoping to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.