-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclockserver.py
More file actions
36 lines (25 loc) · 796 Bytes
/
clockserver.py
File metadata and controls
36 lines (25 loc) · 796 Bytes
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
import select, sys, socket, datetime
# define a socket, set to a non-blocking one
svr_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
svr_sock.setblocking(0)
svr_sock.bind(('localhost', 10000))
svr_sock.listen(5)
inputs = [svr_sock]
outputs = []
while True:
# tigger for in/out/error events.
in_queue, out_queue, err_queue = select.select(inputs, outputs, inputs)
for i in in_queue:
if i is svr_sock:
# in case connection created
clt_sock, clt_addr = svr_sock.accept()
clt_sock.setblocking(0)
print "connection established from %s:%i" % clt_addr
inputs.append(clt_sock)
outputs.append(clt_sock)
for i in out_queue:
data = str(datetime.datetime.now())
i.send(data)
if i in outputs: outputs.remove(i)
if i in inputs: inputs.remove(i)
i.close()