I have two linux computers with fixed IP addresses:
- A print server, whereby the connected printer is shared via CUPS.
(The server has the IP address "192.168.1.2" and the printer is called "test_printer".) - A computer, on which a python application is running, that should be able to use this print server.
Unfortunately, the printer propagation via CUPS seems not to work reliably (possibly due to the structure of the network).
Can I send print jobs directly from a python program to the CUPS print server?
If so, can you please provide a small example?
In theory, I would just send correctly formatted data to the IP address + port, but I didn't get it to work ...
Here are the approaches I have found so far and my problems with them:
command 'lpr'
import subprocess lpr = subprocess.Popen("usr/bin/lpr", stdin=subprocess.PIPE) # on some distros the command is 'lp' lpr.stdin.write("hello world\n") lpr.stdin.close()
Relies on the printer propagation via CUPS.
python module pycups
import cups with open("/home/user/Documents/some.txt", "w") as f: f.write("hello world\n") conn = cups.Connection() conn.printFile("test_printer", "/home/user/Documents/some.txt", "some_title", {})
Before I can use a printer, I'll have to add it first, which in turn relies on the propagation via CUPS.
Also I didn't getconn.addPrinter()
to work.
python module python-escpos / python-printer-escpos
import escpos.printer p = escpos.printer.Network("192.168.1.2", port=631) # port 9100 seems not to work. p.text("hello world\n") p.close()
Probably the most promising approach ... unfortunately it doesn't print anything and throws an exception on closing.
# The traceback was produced in the interactive shell. Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/user/.local/lib/python3.6/site-package/escpos/printer.py", line 214, in close self.device.shutdown(socket.SHUT_RDWR) OSError: [Errno 107] Transport endpoint is not connected
- python module pkipplib / pyipptool
Unfortunately, there seems not to be a working python3 library that implements the Internet Printing Protocol (IPP) in 2018.
I use python 3.6.7.
The print server uses CUPS 2.2.1.