faqts : Computers : Programming : Languages : Python : Snippets : Stream Manipulation

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

26 of 26 people (100%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How do you access the printer from Python under Linux???

Oct 30th, 2000 15:52
Donovan Baarda, Dave Berry,


This question is very open. There are three main levels at which you 
can access the printer under linux; user, device, and IO port.

The usual user level access is to print documents using the 'lpr' 
command. This will spool documents for printing in /var/spool/lpr to be 
printed in order when the printer is available, and allows multiple 
users to print documents without causing conflicts. This requires that 
the Linux box has lpd configured and running. Typicaly lpd is 
configured to use something like magic-filter to automaticly convert 
different types of documents into the format understood by the printer. 
This means most standard file types (postscript, png, text, etc) can be 
printed directly. (Note that there are alternatives to lpd, such as 
cups, that perform basicly the same thing). From Python, things can be 
printed as follows;

  import os

  filename='~/file.ps'
  os.system('lpr %s' % filename)

or 

  p=os.popen('lpr','w')
  p.write('printing test text string\n')
  p.close()

Device level access involves directly opening the linux device file and 
writing to it. This requires that the user has write level access to 
the device, and does not allow shared access to the printer. This is 
not normaly what you want to do unless you are writing something like 
your own lpd replacement. Guru's might be able to do some ioctl magic 
on the device to do things like get the printer status, but otherwise 
this is pretty simple;

  p=os.open('/dev/lp1','w')
  p.write('printing text test string\n')
  p.close()

IO port level access is the lowest level. You do not want to do this 
unless you really want to do wierd things with your printer port. An 
example of this might be plugging in some strange home-built hardware. 
There are a few ways to do this, but the easiest is using the 
linux '/dev/port' device that allows direct access to IO ports. The 
user must have write access to this device. WARNING!!! making a mistake 
when accessing '/dev/port' can seriously stuff up your system! You must 
know exactly what you are doing when you use this device. I probably 
shouldn't be writing this, because if you know enough to try this, you 
probably already know how :-)

    IOports=open("/dev/port","r+b",0)
    def GetChar(address):
        IOports.seek(address)
        return ord(IOports.read(1))
    def PutChar(address,c):
        IOports.seek(address)
        IOports.write(chr(c))

    class lpt:
        def __init__(self,port=0x378):
            self.address=port
        def Put_Data(self,c):
            PutChar(self.address,c)
        def Get_Data(self):
            return GetChar(self.address)
        def Put_Status(self,c):
            PutChar(self.address+1,c)
        def Get_Status(self):
            return GetChar(self.address+1)
        def Put_Control(self,c):
            PutChar(self.address+2,c)
        def Get_Control(self):
            return GetChar(self.address+2)
        
    p=lpt()
    p.Put_Data('a')
    c=p.Get_Status()
    IOPorts.close()