#! /usr/bin/python """ Name : lpr.py Author : David Boddie Created : Tue 08th August 2000 Last modified : Thu 06th September 2001 Purpose : Send a file to a printer queue. """ import os, socket, string, sys def find_switch(l, switch): j = -1 for i in range(0, len(l)): if l[i] == switch: if i+1 < len(l): return (l[i], l[i+1]) else: return (l[i], None) elif l[i][:len(switch)] == switch: return l[i][:len(switch)], l[i][len(switch):] return (None, None) def remove_switch(l, switch): argv = [] i = 0 while i < len(l): if l[i] == switch: i = i + 2 elif l[i][:len(switch)] == switch: i = i + 1 else: argv.append(l[i]) i = i + 1 return argv def getenv(var): if sys.platform == 'RISCOS': return os.getenv(var) else: try: return os.environ[var] except KeyError: return None # Main if len(sys.argv) < 2: print 'lpr [-Pprinter] ' sys.exit(0) argv = sys.argv[1:] P, printer = find_switch(argv, "-P") # Remove the switch and argument from the list argv = remove_switch(argv, "-P") if len(argv) > 0: filename = argv[-1] else: print "lpr [-Pprinter] " sys.exit(0) if printer == None: printer = getenv('PRINTER') if printer == None: print 'No default printer specified: use system variable PRINTER or the -P switch' sys.exit(0) printer = string.strip(printer) try: file = open(filename) except: print 'Could not find '+filename sys.exit(0) # Determine the length of the file # Go to the end of the file file.seek(0, 2) length = file.tell() # Return to the beginning of the file file.seek(0, 0) server = getenv('PRINTER_SERVER') if server == None: print 'No printer server specified: use system variable PRINTER_SERVER' sys.exit(0) server = string.strip(server) #host = os.getenv('HOST') # #if host == None: # print 'No host specified: use system variable HOST' # sys.exit(0) # #host = string.strip(host) host = socket.gethostname() user = getenv('USER') if user == None: print 'No user specified: use system variable USER' sys.exit(0) user = string.strip(user) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #bound = 0 #for port in range(721,732): # # try: # s.bind((socket.gethostname(), port)) # bound = 1 # break # except: # pass # #if bound == 0: # print "Could not bind socket to port." # sys.exit(0) s.connect((server, 515)) # Expect to recieve a job # print "Expect to recieve a job" s.send("\002"+printer+"\012") d = s.recv(1024) if (d != "\000"): print string.strip(d) s.send("\001\012") s.close() sys.exit(0) control = 'H'+host[:31]+"\012"+'N'+filename[:131]+"\012"+'P'+user[:31]+"\012"+'o'+filename[:131]+"\012" # Send the receive control file subcommand # print "Send the receive control file subcommand" s.send("\002"+str(len(control))+" cfA000"+host+"\012") d = s.recv(1024) if (d != "\000"): print string.strip(d) s.send("\001\012") s.close() sys.exit(0) # Send the control file # print "Send the control file" s.send(control) s.send("\000") # Wait for reply # print "Wait for reply" d = s.recv(1024) if (d != "\000"): print string.strip(d) s.send("\001\012") s.close() sys.exit(0) # Send the receive data file subcommand # print "Send the receive data file subcommand" s.send("\003"+str(length)+" dfA000"+host+"\012") d = s.recv(1024) if (d != "\000"): print string.strip(d) s.send("\001\012") s.close() sys.exit(0) # Send the data file # print "Send the data file" while 1: contents = file.readline() if not contents: break s.send(contents) file.close() s.send("\000") # Wait for reply # print "Wait for reply" d = s.recv(1024) if (d != "\000"): print string.strip(d) s.send("\001\012") s.close() sys.exit(0) s.close() sys.exit(0)