[paramiko] How to use settimeout with from_transport
James Bardin
jbardin at bu.edu
Fri Apr 27 15:50:28 PDT 2007
Hi Robey,
Just passing this along if you want to use it.
I needed that ConnectTimeout option from openssh, so I added an
SSHClient.set_connect_timeout() method that sets the socket timeout
during the first connect, then transport takes over.
Another idea would be to move transport's connect outside of the
constructor, and give it a separate connect method, but this was faster
for my needs.
Thanks!
-jim
=== modified file 'paramiko/client.py'
--- paramiko/client.py 2007-04-27 22:24:16 +0000
+++ paramiko/client.py 2007-04-27 22:34:07 +0000
@@ -23,6 +23,7 @@
from binascii import hexlify
import getpass
import os
+import socket
from paramiko.agent import Agent
from paramiko.common import *
@@ -109,7 +110,8 @@
self._log_channel = None
self._policy = RejectPolicy()
self._transport = None
-
+ self._connect_timeout = None
+
def load_system_host_keys(self, filename=None):
"""
Load host keys from a system (read-only) file. Host keys read with
@@ -211,6 +213,16 @@
"""
self._policy = policy
+ def set_connect_timeout(self, timeout):
+ """
+ Set the socket time out for the initial connection.
+ equivalent to ConnectTimeout option in openssh
+
+ @param timeout: The timeout in seconds for the initial connection
+ @type timeout: int
+ """
+ self._connect_timeout = timeout
+
def connect(self, hostname, port=22, username=None, password=None,
pkey=None,
key_filename=None):
"""
@@ -253,7 +265,16 @@
@raise SSHException: if there was any other error connecting or
establishing an SSH session
"""
- t = self._transport = Transport((hostname, port))
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+
+ if self._connect_timeout:
+ try:
+ sock.settimeout(self._connect_timeout)
+ except:
+ pass
+
+ sock.connect((hostname, port))
+ t = self._transport = Transport(sock)
if self._log_channel is not None:
t.set_log_channel(self._log_channel)
t.start_client()
More information about the paramiko
mailing list