title: `sock.recv()` returns empty string when connection is dead on non-blocking socket
body: <p>I have a non-blocking socket in Python called <code>sock</code>. According to my understanding the <code>recv()</code> method should raise an exception if the connection has been closed by the peer, but it returns an empty string (<code>''</code>) and I don't know why.</p>
  
  <p>This is the script I test with (from <a href="https://stackoverflow.com/questions/16745409/what-does-pythons-socket-recv-return-for-non-blocking-sockets-if-no-data-is-r">here</a>):</p>
  
  <pre><code>import sys
  import socket
  import fcntl, os
  import errno
  from time import sleep
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect(('127.0.0.1',9999))
  fcntl.fcntl(s, fcntl.F_SETFL, os.O_NONBLOCK)
  
  while True:
      try:
          msg = s.recv(4096)
          print("got data '{msg}'".format(msg=msg))
      except socket.error, e:
          err = e.args[0]
          if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
              sleep(1)
              print 'No data available'
              continue
          sys.exit(1)
  </code></pre>
  
  <p>If the peer closes the connection this socket is supposed to raise <code>socket.error</code> on <code>recv()</code>, but instead it only returns <code>''</code>.</p>
  
  <p>I test it this way, using two terminals:</p>
  
  <pre><code># Terminal 1
  ~$ nc -l -p9999
  
  # Terminal 2
  ~$ python ./test_script.py
  
  # Terminal 1
  typingsomestring
  
  # Terminal 2
  No data available
  No data available
  No data available
  got data 'typingsomestring
  '
  No data available
  No data available
  
  # Terminal 1
  Ctrl+C # (killing nc)
  
  # Terminal 2
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  got data ''
  </code></pre>