title: Python socket programming - exception handling
body: <p>I'm working on a basic socket client program in python and I'm not totally sure how to handle exceptions. This is what I did up to now:</p>
  
  <pre><code>TCP_IP      = '..............'
  TCP_PORT    = 4950
  MESSAGE     = "o3"
  BUFFER_SIZE = 2048
  data        = ""
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
  s.settimeout(5.0)
  
  try:
      s.connect((TCP_IP, TCP_PORT))
  except socket.error:
      #write error code to file
      s.close()
  
  try:
      s.sendall(MESSAGE)
  except socket.error:
      #write to file or whatever
      s.close()
  
  try:
      data = s.recv(BUFFER_SIZE)
  except socket.error:
      #write to file or whatever
      s.close()
  
  finally:
      s.close()
  </code></pre>
  
  <p>The code is working as I want, but I'm not sure if I should nest try/catch blocks or not? Should I put <code>socket.socket</code> into try/catch block too?</p>
  
  <p>Second question, what will <code>s.settimeout()</code> do in my case? As far as I understood the documentation, it will throw an exception after 5 seconds, but for what? Just <code>connect</code> or will it do the same for <code>sendall</code> and <code>recv</code>? </p>