0

I am using a python script as a TCP client. At one point it receives what should be a bool back from the server. When I was first writing this and testing it on a machine that was using a regular python install I was able to use the following code from the struct module to unpack the data received back as a bool

data = socket.recv(BUFFER_SIZE)
result = struct.unpack("?", data)[0]
if not result:
    #do stuff here

I currently need to perform the same task in an environment that only uses ironpython. When I attempt to run the code I get the following:

ImportError: No module named struct

After a bit of digging it seems that the struct module in regular python doesn't exist in ironpython (I believe a struct module exists, but it is for interfacing with c/c++ structs)

How can I take the data from a TCP socket.recv and convert it from the bytes format actually received to the bool type I need it to be in ironpython?

Tuffwer
  • 1,027
  • 9
  • 23

1 Answers1

1

After some more digging I came across this post which was asking about converting a string of bytes to an int.

I found the solution offered in this answer avoided having to import any modules and worked quite well, I needed to perform an additional conversion to bool so my final result looks like this:

data = sock.recv(BUFFER_SIZE)
result = bool(int(data.encode('hex'), 16))
Community
  • 1
  • 1
Tuffwer
  • 1,027
  • 9
  • 23