Listing 3. time.client.py
#!/usr/bin/python
#
# time.client.py - gets the time from a time server.
#
# Thu Nov 8 14:10:49 PST 2001 Rory Krause
#
# Usage: ./time.client.py portnumber
#
# Simple time client that takes first argument
# as a port number, connects to that port and
# prints the time to standard out. Use with
# time.server.py.
#
from socket import *
import sys
port = int(sys.argv[1]) # Get port number
s = socket(AF_INET, SOCK_STREAM) # Create TCP socket
s.connect(("localhost", port)) # Connect to port
data = s.recv(1024) # Receive up to
# 1024 bytes
s.close() # Close connection
print "The time is ", data # Print time
Copyright © 1994 - 2019 Linux Journal. All rights reserved.