When I run the following python example code,
tick = 0
while True:
tick += 1
print tick
data = s.recv(1024)
if (tick == 1) and data:
print 'from client: %s' %(data)
elif (tick == 2) and data:
print 'from client: %s' %(data)
I see,
1
from client: client msg
2
from client: ?
3
My intuition tells me the 2nd call to s.recv() actually returns some data. And I am fairly certain the client is not sending the `?' character.
So I modify the code hoping to print the first byte of `data',
elif (tick == 2) and data:
print 'from client: %s' %(data)
print struct.unpack("!B", data)
But then I get a traceback stating: "struct.error: unpack requires a string argument of length 1."
The struct package seems to be the standard way of handling socket data. However, this situation seems odd. I am receiving data visually by printing and seeing a "?" and the code also has an "and data" in the conditional but I cannot unpack.
Is there a different way to handle binary data off a socket?