Listing 4. time.server.py
#!/usr/bin/python
#
# time.server.py - provides a time service.
#
# Thu Nov 8 14:10:49 PST 2001 Rory Krause
#
# Usage: ./time.server.py portnumber
#
# Simple time server that takes first argument
# as a port number, listens for connections and
# sends out the time. Use with time.client.py.
#
from socket import *
import time, sys
port = int(sys.argv[1]) # Get port number
s = socket(AF_INET,SOCK_STREAM) # Create TCP socket
s.bind(("",port)) # Bind to port
s.listen(1) # Listen, but allow
# only 1 connection
while 1:
client,addr = s.accept() #Get a connection
print "Got a connection from ",addr
# Send time to client
client.send(time.ctime(time.time()))
client.close()