-
Notifications
You must be signed in to change notification settings - Fork 2
/
asyncore-example-1.py
33 lines (33 loc) · 1.15 KB
/
asyncore-example-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import asyncore
import socket, time
# reference time (in seconds since 1900-01-01 00:00:00)
TIME1970 = 2208988800L # 1970-01-01 00:00:00
class TimeRequest(asyncore.dispatcher):
# time requestor (as defined in RFC 868)
def __init__(self, host, port=37):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host, port))
def writable(self):
return 0 # don't have anything to write
def handle_connect(self):
pass # connection succeeded
def handle_expt(self):
self.close() # connection failed, shutdown
def handle_read(self):
# get local time
here = int(time.time()) + TIME1970
# get and unpack server time
s = self.recv(4)
there = ord(s[3]) + (ord(s[2])<<8) + (ord(s[1])<<16) + (ord(s[0])<<24L)
self.adjust_time(int(here - there))
self.handle_close() # we don't expect more data
def handle_close(self):
self.close()
def adjust_time(self, delta):
# override this method!
print "time difference is", delta
#
# try it out
request = TimeRequest("localhost")
asyncore.loop()