title: Python - is finally block necessary in socket operation?
body: <p>I am a newbie to Python and currently doing some socket development. I am not very sure about the best practices of Python socket programming.</p>
  
  <p>For example, in the following code, are the <code>conn=None</code> line and the <code>finally</code> block necessary? What is the best convention for this kind of operation?</p>
  
  <pre><code>@post('/relay/')
  def relay():
      conn = None  # Do I need this line to ensure conn not to be undefined?
      try:
          conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          conn.connect(host_port)
          conn.sendall(content)
      except socket.error, se:
          if se.errno == 61:
              logger.error(...)
          else:
              logger.error(...)
          abort(400, 'Error...')
      finally:   # Should this socket.close() task be in finally block? like Java?
          if conn:
              conn.shutdown(socket.SHUT_RDWR)
              conn.close()
      return ''
  </code></pre>