5462l2 Listing 2. rlclient #!/usr/bin/python # # rlclient - remote lpr client # # Thu Nov 8 11:06:32 PST 2001 Rory Krause # # Usage: rlclient # # Connects to local port and checks to see if there # is an incoming print job. If there is, the data # is written to a temporary file and then sent to # lpr. from socket import * from time import ctime, time import os, signal, tempfile, sys def handler(signum, frame): print "Polling for print jobs. " + ctime(time()) try: # Create a TCP socket s = socket(AF_INET, SOCK_STREAM) # Bind to port 8888 # NOTE: this port is hard coded. # You may want to change this. s.connect(("localhost", 8888)) except: typ,val = sys.exc_info()[:2] typname = str(type) valname = str(val) print "Could not connect: " + valname #print "typname: " + typname #print "valname: " + valname else: if s: print "socket exists" data = s.recv(1024) # Receive data if data: filename = tempfile.mktemp() print filename f = open(filename, "w") f.write(data) f.flush() # Get the rest of the data while 1: # Receive a chunk of data data = s.recv(1024) # If there is no more data if not data: # then we are done break # For debuging print data # Write out to temp file f.write(data) # Please remember to flush f.flush() # Close the temp file f.close() # Time to print this junk print "Sending to lpr" os.system("lpr " + filename) else: print "no data" # Close socket connection s.close signal.signal(signal.SIGALRM, handler) while 1: signal.alarm(5) signal.pause()