I have created a personal POS application which is sending a JSON object via HTTP Request. UI of the POS app:
When buttons Pay or Dispense are clicked then a json object is sent via POST method. I have created a draft HTTP server in python just to receive the sent data. Here is the code:
from http.server import BaseHTTPRequestHandler, HTTPServer
hostName = "localhost"
serverPort = 49231
class MyServer(BaseHTTPRequestHandler):
def do_POST(self):
print(self.headers)
content_len = int(self.headers.get('content-length', 0))
post_body = self.rfile.read(content_len)
post_body = post_body.decode('utf-8')
if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)
print("Server started http://%s:%s" % (hostName, serverPort))
try:
webServer.serve_forever()
except KeyboardInterrupt:
webServer.server_close()
print("Server stopped.")
In post_body variable I have the json sent from the POS app. My idea was to encode the json string into a qr code then print the qr code so as to take the paper with the qr code printed and scan it. I used the qrcode module to create the QR code. My simple function to generate QR code from string:
import tempfile
import qrcode
def generate_qr(qr_input):
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmpfile:
qr = qrcode.make(qr_input)
qr.save(tmpfile, qr.format, quality=100)
return open(tmpfile.name, 'rb'), 'image/png'
For the silent print I found this, where with the help of win32api and win32print modules, we can execute a silent print. My problem is that win32api.ShellExecute documentation says that we need to have a document file in order to execute properly this method. Now my custom generator of QR code is returning a tuple with the temp file. I am using also tempfile module, as I don't want every time that the button is clicked to create a new file containing a QR code. My question is how can I generate a document file from a temp PNG file in python?
Finally, as this code is produced for a university project, any new idea on how to execute silent print would be welcome.
My complete code:
import tempfile
import win32api
import win32print
from http.server import BaseHTTPRequestHandler, HTTPServer
import qrcode
hostName = "localhost"
serverPort = 49231
def generate_qr(qr_input):
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmpfile:
qr = qrcode.make(qr_input)
qr.save(tmpfile, qr.format, quality=100)
return open(tmpfile.name, 'rb'), 'image/png'
class MyServer(BaseHTTPRequestHandler):
def do_POST(self):
print(self.headers)
# print_qr = {}
content_len = int(self.headers.get('content-length', 0))
post_body = self.rfile.read(content_len)
post_body = post_body.decode('utf-8')
print(post_body)
print_qr = generate_qr(post_body)
win32api.ShellExecute(
0,
'print',
print_qr,
'"%s"' % win32print.GetDefaultPrinter(),
'.',
0
)
if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)
print("Server started http://%s:%s" % (hostName, serverPort))
try:
webServer.serve_forever()
except KeyboardInterrupt:
webServer.server_close()
print("Server stopped.")