title: Netcat implementation in Python
body: <p>I found <a href="http://4thmouse.com/index.php/2008/02/22/netcat-clone-in-three-languages-part-ii-python/" rel="noreferrer">this</a> and am using it as my base, but it wasn't working right out of the box. My goal is also to treat it as a package instead of a command line utility, so my code changes will reflect that.</p>
  
  <pre><code>class Netcat:
      def __init__(self, hostname, port):
          self.hostname = hostname
          self.port = port
      def send(self, content):
      self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      self.socket.connect((self.hostname, self.port))
      self.socket.setblocking(0)
      result = '';
      read_ready, write_ready, in_error = select.select([self.socket], [], [self.socket], 5)
      if(self.socket.sendall(content) != None):
          return
      while(1):
          buffer = ''
          try:                
              buffer = self.socket.recv(128)
              while(buffer != ''):
                  result += buffer
                  try:
                      buffer = self.socket.recv(128)
                  except socket.error as err:
                      print (err, type(err))
                      buffer = ''
              if(buffer == ''):
                  break
          except socket.error as err:
              print (err, type(err))
          if(buffer == ''):
              break
      return result
  </code></pre>
  
  <p>When I send a basic command to my device, it returns the following.</p>
  
  <pre><code>50PMA-019 Connection Open
  Atten #1 = 63dB
  </code></pre>
  
  <p>My code reads the first line, but then I get an error saying that the connection is temporarily unavailable and it does not get the second line. If I change it to blocking, it just blocks and never returns. Any thoughts?</p>