0

is there a cleaner way to do this? I am interrogating a sensor on Ethernet, and I only need the first of the values given converted in a string or a number (the sensor is a counter).

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((IP_address,5400))
s.sendall(b'gcounters\x00')
L_IN = list(struct.unpack('4B',(s.recv(1024))))
L_OUT = list(struct.unpack('4B',(s.recv(1024))))
s.close()

## Deleting everything after the first value [x, 0, 0, 0]

del L_IN[1:4]
del L_OUT[1:4]

Can you help me to clean out the code please?

  • Why unpack four values if you only want one of them? If you just unpack one of them, you can follow up with [the code here for unpacking a single value cheaply](https://stackoverflow.com/q/33161448/364696). – ShadowRanger Jul 02 '20 at 00:41

1 Answers1

0

Thank you so much!

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((IP_address,5400))
s.sendall(b'gcounters\x00')
L_IN = next(iter(s.recv(1024)))
L_OUT = next(iter(s.recv(1024)))
s.close()

It worked!