Trying to send commands (not labels) to Zebra printers using Python.
On page 574 of the documentation it shows:
Here's my code:
mysocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host= "192.168.100.245" # verified IP address of Zebra printer
port = 9100
mysocket.connect((host, port))
name_string= '''"sgd.name":null'''
my_string= f'''{{}}{{{name_string}}}'''
x = json.dumps(obj=my_string)
mysocket.sendall(bytes(x,encoding="utf-8"))
data= mysocket.recv(1024)
print(data.decode('utf-8'))
The printer responds to pings and other non-JSON Zebra commands sent to it (i.e. mysocket.send(b"~hs")
). However, with the code above I wait for a long time and no response returns from the printer.
Tried multiple variations of the JSON formatting, what should I try next?
Per @bruan comment I tried the following variations but did not work:
my_string= '''"sgd.name":null"'''
my_string= '''{}{"sgd.name":null}'''
You create the JSON/command manually, then dump it into JSON one more time. What you send now is '"{}{\\"sgd.name\\":null}"'
, i.e. the string "{}{\\"sgd.name\\":null}"
Also, as I said in the comments, better don't create JSON manually. Use json
module and it will be easier to expand the query
dict and get multiple values at once if needed.
import json
# some lines skipped for brevity
query = {'sgd.name': None}
command = f'{{}}{json.dumps(query)}' # {}{"sgd.name": null}
mysocket.sendall(bytes(command, encoding="utf-8"))