title: Python telnet to download instrument data
body: <p>I would like to write some python code to download data from a pressure sensor we have, a <a href="http://www.vaisala.com/Vaisala%20Documents/User%20Guides%20and%20Quick%20Ref%20Guides/PTU300%20User%20Guide%20in%20English.pdf" rel="nofollow noreferrer">Vaisala PTU300</a>. This sensor connects to our router and can be contacted via a telnet session where simple commands spit out data on request, or it can spit out a continuous stream of data. Page 94 of the manual lists the commands you can send to the instrument. I'm quite new to python but do have some programming experience in Fortran so understand some structuring of programming languages. </p>
  
  <p>I was hoping someone could help me with some ideas of what modules i should implement to do this. I've done a little research on the topic and found these past questions which have some relevance (<a href="https://stackoverflow.com/questions/630217/how-can-i-use-pythons-telnetlib-to-fetch-data-from-a-device-for-a-fixed-period">How can I use python's telnetlib to fetch data from a device for a fixed period of time?</a>, <a href="https://stackoverflow.com/questions/14644402/how-to-save-the-output-of-a-telnet-command-by-using-python-for-an-infinite-time">How to save the output of a telnet command by using python for an infinite time [closed]</a>). </p>
  
  <p>I have started to play with trying to use telnetlib to connect to the instrument with the following commands</p>
  
  <p>import sys</p>
  
  <p>import telnetlib</p>
  
  <p>host = 'xxx.xxx.xxx.xxx'</p>
  
  <p>tn=telnetlib.Telnet(host)</p>
  
  <p>tn.open</p>
  
  <p>tn.write('r')</p>
  
  <p>tn.read_very_eager()</p>
  
  <p>This results in one line of data being read </p>
  
  <p>e.g.</p>
  
  <p>2000-01-06 23:43:04 P=    980.0  T= -4.3 'C \r\n>r</p>
  
  <p>however subsequent tn.read_very_eager() does not return anything. </p>
  
  <p>I have tried different [variations][4] on this read command, e.g. </p>
  
  <p>tn.read_until('C') </p>
  
  <p>but any subsequent reads would not return anything. I believe some "blocking" might be occurring? not that i really know what that is. </p>
  
  <p>I have also tried requesting just one reading using</p>
  
  <p>tn.write('send')</p>
  
  <p>tn.read_until('C')</p>
  
  <p>but this would only return one reading and subsequent reads would return nothing. </p>
  
  <p>[One][5] of the past stackoverflow questions suggested using the [asynchat][6] or [socket][7] modules to perform this kind of action. I have not tried using them yet. Do people think this is a better line of attack? The instrument requires no username or password to connect to it. </p>
  
  <p>So my question would be, how can i best connect to this instrument (which package, telnetlib, asynchat, socket)?</p>
  
  <p>How can I avoid the telnet connection being blocked? (i.e. only being able to retrieve one line of data)?</p>
  
  <p>Any advice would be greatly appreciated, i appologise if this question has already been answered. </p>
  
  <p>Regards</p>
  
  <p>Oman</p>
  
  <p>ps, sorry it wouldn't allow me to attach all my links</p>