computer, travel, movies, music, cuisine and more
Posts tagged python
TIME protocol on MacOSX
Apr 15th
Recently in the Advanced Operating System course, at ETH (Systems) I’ve struggled upon the TIME protocol (not NTP). The problem was simple: get a TIME protocol server that could respond to the SLUG2 (friendly called by me and my project partner SLUT) when ‘she’ was needing a reply (for some randomness, apparently).
Since MacOSX doesn’t provide any such thing, I settled on the task and here’s the result:
Server side:
#!/usr/bin/env python
import time
from socket import *
def server(host="", port=37):
sock = socket (AF_INET, SOCK_DGRAM)
sock.bind ((host, port))
print "listening on port %s (%s)" % (port, `host`)
while 1:
# Block waiting for packet.
data, address = sock.recvfrom(256)
print "Client sent:", data
print "Client at:", address
# Got a packet, reply to address packet came from.
sock.sendto(time.time(),address)
if __name__ == "__main__":
server("", 37)
Client side (just to test that the server is actually working)
#!/usr/bin/env python
import socket
port=37
clisocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while 1:
data = raw_input("Type something: ")
if data:
clisocket.sendto(data, ("127.0.0.1", port))
# Block waiting for reply.
data, address = clisocket.recvfrom(256)
print "Server sent time:", data
else:
break
clisocket.close()
This code (and any other that might be present on this blog) should be taken with a good grain of salt. It seems to do what it’s supposed to do. So if you’re in an emergency, trying to find a TIME protocol server to make it answer your client needs, here it is. Fiddle with it as much as you want. And don’t hesitate to comment with good and bad news about it.
The code and the issue have been tested on MacOSX 10.5.6
ciop ciop