I’m connecting to a hardware device via telnet. That device is pretty simple in terms of I/O. So I submit a command to it, and after that the device pumps out data one line at a time, once per second. Each line just contains a number.
So my question is this: if I connect to this device using python’s telnetlib, how can I fetch data for a fixed period of time (or a fixed number of lines of data)?
I’ve tried using all the various read_ commands, but they all seem to block indefinitely, apart from read_until, which I can’t use as the output can’t be used to determine when to stop.
(I’m running python 2.5 under Cygwin, btw).
Edit: Maybe the real question is, should I be using telnetlib at all for this, or should I just use the socket module?
From your description I’m not clear if you’re using telnetlib because the device you’re connecting to requires terminal setup provided by telnet or because it seemed like the right thing to do.
If the device is as simple as you describe–i.e. not negotiating terminal options on connection–have you considered the asynchat module? It would be appropriate for the ‘send command, read lines’ sort of IO pattern you are describing.
Alternatively, for something lower-level, you could use the socket module to open the connection to the device and then sit in a timed loop and
read()the incoming data. If the lines are newline-terminated it should be simple for you to identify each individual number. If you are concerned with blocking, stick this loop in its own thread.