title: Can I send print jobs directly from a python program to the IP address of a CUPS print server?
body: <p>I have two linux computers with fixed IP addresses:</p>
  
  <ol>
  <li>A print server, whereby the connected printer is shared via <a href="https://www.cups.org/" rel="noreferrer">CUPS</a>.<br>
  (The server has the IP address "192.168.1.2" and the printer is called "test_printer".)</li>
  <li>A computer, on which a python application is running, that should be able to use this print server.</li>
  </ol>
  
  <p>Unfortunately, the printer propagation via CUPS seems not to work reliably (possibly due to the structure of the network).</p>
  
  <p><strong>Can I send print jobs directly from a python program to the CUPS print server?<br>
  If so, can you please provide a small example?</strong></p>
  
  <p>In theory, I would just send correctly formatted data to the IP address + port, but I didn't get it to work ...</p>
  
  <p><br></p>
  
  <p>Here are the approaches I have found so far and my problems with them:</p>
  
  <ul>
  <li><p><strong>command 'lpr'</strong>  </p>
  
  <pre><code>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()
  </code></pre>
  
  <p>Relies on the printer propagation via CUPS.</p></li>
  </ul>
  
  <p><br></p>
  
  <ul>
  <li><p><strong>python module <a href="https://pypi.org/project/pycups/" rel="noreferrer">pycups</a></strong>  </p>
  
  <pre><code>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", {})
  </code></pre>
  
  <p>Before I can use a printer, I'll have to add it first, which in turn relies on the propagation via CUPS.<br>
  Also I didn't get <code>conn.addPrinter()</code> to work.</p></li>
  </ul>
  
  <p><br></p>
  
  <ul>
  <li><p><strong>python module <a href="https://pypi.org/project/python-escpos/" rel="noreferrer">python-escpos</a> / <a href="https://pypi.org/project/python-printer-escpos/" rel="noreferrer">python-printer-escpos</a></strong>  </p>
  
  <pre><code>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()
  </code></pre>
  
  <p>Probably the most promising approach ... unfortunately it doesn't print anything and throws an exception on closing.</p>
  
  <pre><code># The traceback was produced in the interactive shell.
  Traceback (most recent call last):
      File "&lt;stdin&gt;", line 1, in &lt;module&gt;
      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
  </code></pre></li>
  </ul>
  
  <p><br></p>
  
  <ul>
  <li><strong>python module <a href="https://pypi.org/project/pkipplib/" rel="noreferrer">pkipplib</a> / <a href="https://pypi.org/project/pyipptool/" rel="noreferrer">pyipptool</a></strong><br>
  Unfortunately, there seems not to be a working python3 library that implements the <a href="https://en.wikipedia.org/wiki/Internet_Printing_Protocol" rel="noreferrer">Internet Printing Protocol (IPP)</a> in 2018.</li>
  </ul>
  
  <hr>
  
  <p><em>I use python 3.6.7.</em><br>
  <em>The print server uses CUPS 2.2.1.</em></p>