Listing 1. rlserver

#!/usr/bin/python
#
# rlserver - remote lpr server
#
# Thu Nov  8 11:06:32 PST 2001 Rory Krause
#
# Usage: rlserver portnumber
#
# Gets "printjob" data from standard in and fires
# up a server on port ARG 1.  The server waits for
# a connection and then passes on the data to the
# connecting client program.

from socket import *
from os import remove
import sys, tempfile

data = sys.stdin.read()
filename = tempfile.mktemp()
f = open(filename, "w")
f.write(data)
f.close()        # Close file to make sure all
                 # buffered data has been written

port = int(sys.argv[1]) # read port number from argv
s = socket(AF_INET, SOCK_STREAM)# Create TCP socket
s.bind(("",port))               # Bind to port
s.listen(1)                     # Listen, but allow
                                #   only 1 connection
print "Listening on port: " + str(port)
client,addr = s.accept()        # Accept a connection
print "Got a connection from ",addr
z = open(filename, "r")
while 1:
    bunch = z.read(512)
    if bunch:
        sent = client.send(bunch)
        print len(bunch)
        z.seek(sent-len(bunch),1)
    else:
        z.close()               # Close temp file
        break                   # Let's get out of
                                #   this while loop
client.close()                  # Close client
                                #   connection
s.close()                       # Close the socket