title: how to handle socket errors as exceptions in python + paramiko?
body: <p>I want to return an error code when the following error gets raised:</p>
  
  <pre><code>Traceback (most recent call last):
   File "&lt;stdin&gt;", line 1, in &lt;module&gt;
   File "UserManagementRemote.py", line 202, in create_group
    ssh.connect(hostname, username=user, password=remotepass)
   File "/usr/lib/python2.6/site-packages/paramiko/client.py", line 290, in connect
     sock.connect(addr)
   File "&lt;string&gt;", line 1, in connect
  socket.error: [Errno 113] No route to host
  &gt;&gt;&gt;
  </code></pre>
  
  <p>But I'm currently having trouble catching the error raised.</p>
  
  <pre><code>try:
     ssh = paramiko.SSHClient()
     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     ssh.connect(hostname, username=user, password=remotepass)
  except paramiko.AuthenticationException:
     return 259
  except socket.error:
     return 261
  chan = ssh.get_transport().open_session()
  chan.exec_command(command)
  codest = chan.recv_exit_status()
  ssh.close()
  return codest
  </code></pre>
  
  <p>Resulting on this:</p>
  
  <pre><code>Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "UserManagementRemote.py", line 207, in create_group
    except socket.error:
  NameError: global name 'socket' is not defined
  &gt;&gt;&gt;
  </code></pre>
  
  <p>Any ideas?</p>