How to send raw string to a dotmatrix printer using python in ubuntu?
<p>I have a dot-matrix printer LX-300 connected to my computer through the network. How do I send a raw string with ESCP characters directly to my printer in Python?</p>

<p>The computer is connected to the printer through another computer. I need to send a raw string because LX-300 image printing result is blurry.</p>

<h2>The Problem</h2>

<p>To send data down this route:</p>

<p>Client computer ---> Server (Windows machine) ---> printer (dot-matrix)</p>

<p>...and to <em>not</em> let Windows mess with the data; instead to send the raw data, including printer control codes, straight from the client computer.</p>

<h2>My Solution</h2>

<p>Here's how I solved a near-identical problem for a small in-house database application:</p>

<p>Step 1) Make the printer network-accessible without Windows getting its fingers in the data routed to it. I accomplished this by installing the printer using the "Generic/Text Only" driver, then installing 
<a href="https://sourceforge.net/projects/rawprintserver/" rel="nofollow">RawPrintServer</a> on the Windows machine connected to the printer.</p>

<p>Step 2) Send raw data over the network to the TCP/IP port specified when you set up RawPrintServer (default is 9100). There are various ways to do that, here's what I did:</p>

<pre><code>data = b"\x1B@A String To Print\x1B@" # be sure to use the right codes for your printer
ip_addr = 123.123.123.123 # address of the machine with the printer
port = 9100 # or whatever you set it to
s = socket.socket()
try:
    s.connect((ip_addr, port))
    s.send(data)
except:
    # deal with the error
finally:
    s.close()
</code></pre>

<h2>Background</h2>

<p>I thought about the problem in two parts:</p>

<ol>
<li>Client machine: spitting out the data I need from Python with the correct formatting/control codes for my printer, and sending it across the network</li>
<li>Print server machine: transmitting the data to the locally connected printer</li>
</ol>

<p>Number 1 is the easy part. There are actually <a href="https://pypi.python.org/pypi?%3Aaction=search&amp;term=escpos&amp;submit=search" rel="nofollow">some libraries in PyPI</a> that may help with all the printer codes, but I found most of them are aimed at the little point-of-sale label printers, and were of limited use to me. So I just hard-coded what I needed into my Python program.</p>

<p>Of course, the way you choose to solve number 2 will effect how you send the data from Python. I chose the TCP/IP route to avoid dealing with Samba and Windows print issues.</p>

<p>As you probably discovered, Windows normally tries very hard to convert whatever you want to print to a bitmap and run the printer in graphics mode. We can use the generic driver and dump the data straight into the (local) printer port in order to prevent this.</p>

<p>The missing link, then, is getting from the network to the local printer port on the machine connected to the printer. Again, there are various ways to solve this. You could attempt to access the Windows printer share in some way. If you go the TCP/IP route like I did, you could write your own print server in Python. In my case, the RawPrintServer program "just worked" so I didn't investigate any further. Apparently all it does is grab incoming data from TCP port 9100 and shove it into the local printer port. Obviously you'll have to be sure the firewall isn't blocking the incoming connections on the print server machine. This method does not require the printer to be "shared" as far as Windows is concerned.</p>

<p>Depending on your situation (if you use DHCP), you might need to do some extra work to get the server's IP address in Python. In my case, I got the IP for free because of the peculiarity of my application.</p>

<p>This solution seems to be working out very well for me. I've got an old Panasonic printer running in Epson ESC/P compatibility mode connected to a Windows 7 machine, which I can print to from any other computer on the local network. Incidentally, this general idea should work regardless of what OS the client computer is running.</p>

python epson dot-matrix escpos