Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for configuring socket_timeout in SCP mode #423

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/SSHLibrary/abstractclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class SSHClientException(RuntimeError):
class _ClientConfiguration(Configuration):

def __init__(self, host, alias, port, timeout, newline, prompt, term_type,
width, height, path_separator, encoding, escape_ansi, encoding_errors):
width, height, path_separator, encoding, escape_ansi, encoding_errors,
socket_timeout):
super(_ClientConfiguration, self).__init__(
index=IntegerEntry(None),
host=StringEntry(host),
Expand All @@ -51,7 +52,8 @@ def __init__(self, host, alias, port, timeout, newline, prompt, term_type,
path_separator=StringEntry(path_separator),
encoding=StringEntry(encoding),
escape_ansi=StringEntry(escape_ansi),
encoding_errors=StringEntry(encoding_errors)
encoding_errors=StringEntry(encoding_errors),
socket_timeout=IntegerEntry(socket_timeout)
)


Expand All @@ -64,10 +66,12 @@ class AbstractSSHClient(object):
"""
def __init__(self, host, alias=None, port=22, timeout=3, newline='LF',
prompt=None, term_type='vt100', width=80, height=24,
path_separator='/', encoding='utf8', escape_ansi=False, encoding_errors='strict'):
path_separator='/', encoding='utf8', escape_ansi=False, encoding_errors='strict',
socket_timeout=10):
self.config = _ClientConfiguration(host, alias, port, timeout, newline,
prompt, term_type, width, height,
path_separator, encoding, escape_ansi, encoding_errors)
path_separator, encoding, escape_ansi, encoding_errors,
socket_timeout)
self._sftp_client = None
self._scp_transfer_client = None
self._scp_all_client = None
Expand Down
38 changes: 28 additions & 10 deletions src/SSHLibrary/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ class SSHLibrary(object):
Argument ``timeout`` is used by `Read Until` variants. The default value
is ``3 seconds``. See `time format` below for supported timeout syntax.

=== Socket timeout ===

Argument ``socket timeout`` defines the timeout for waiting for the individual
packets. The value is specified in seconds and should be integer.
The default is ``10`` seconds.

=== Newline ===

Argument ``newline`` is the line break sequence used by `Write` keyword
Expand Down Expand Up @@ -454,6 +460,7 @@ class SSHLibrary(object):
DEFAULT_ENCODING = 'UTF-8'
DEFAULT_ESCAPE_ANSI = False
DEFAULT_ENCODING_ERRORS = 'strict'
DEFAULT_SOCKET_TIMEOUT = 10

def __init__(self,
timeout=DEFAULT_TIMEOUT,
Expand All @@ -466,7 +473,8 @@ def __init__(self,
path_separator=DEFAULT_PATH_SEPARATOR,
encoding=DEFAULT_ENCODING,
escape_ansi=DEFAULT_ESCAPE_ANSI,
encoding_errors=DEFAULT_ENCODING_ERRORS):
encoding_errors=DEFAULT_ENCODING_ERRORS,
socket_timeout=DEFAULT_SOCKET_TIMEOUT):
"""SSHLibrary allows some import time `configuration`.

If the library is imported without any arguments, the library
Expand Down Expand Up @@ -504,7 +512,8 @@ def __init__(self,
path_separator or self.DEFAULT_PATH_SEPARATOR,
encoding or self.DEFAULT_ENCODING,
escape_ansi or self.DEFAULT_ESCAPE_ANSI,
encoding_errors or self.DEFAULT_ENCODING_ERRORS
encoding_errors or self.DEFAULT_ENCODING_ERRORS,
socket_timeout or self.DEFAULT_SOCKET_TIMEOUT
)
self._last_commands = dict()

Expand All @@ -516,7 +525,8 @@ def current(self):
def set_default_configuration(self, timeout=None, newline=None, prompt=None,
loglevel=None, term_type=None, width=None,
height=None, path_separator=None,
encoding=None, escape_ansi=None, encoding_errors=None):
encoding=None, escape_ansi=None, encoding_errors=None,
socket_timeout=None):
"""Update the default `configuration`.

Please note that using this keyword does not affect the already
Expand Down Expand Up @@ -550,11 +560,13 @@ def set_default_configuration(self, timeout=None, newline=None, prompt=None,
self._config.update(timeout=timeout, newline=newline, prompt=prompt,
loglevel=loglevel, term_type=term_type, width=width,
height=height, path_separator=path_separator,
encoding=encoding, escape_ansi=escape_ansi, encoding_errors=encoding_errors)
encoding=encoding, escape_ansi=escape_ansi, encoding_errors=encoding_errors,
socket_timeout=socket_timeout)

def set_client_configuration(self, timeout=None, newline=None, prompt=None,
term_type=None, width=None, height=None,
path_separator=None, encoding=None, escape_ansi=None, encoding_errors=None):
path_separator=None, encoding=None, escape_ansi=None, encoding_errors=None,
socket_timeout=None):
"""Update the `configuration` of the current connection.

Only parameters whose value is other than ``None`` are updated.
Expand Down Expand Up @@ -590,7 +602,8 @@ def set_client_configuration(self, timeout=None, newline=None, prompt=None,
width=width, height=height,
path_separator=path_separator,
encoding=encoding, escape_ansi=escape_ansi,
encoding_errors=encoding_errors)
encoding_errors=encoding_errors,
socket_timeout=socket_timeout)

def enable_ssh_logging(self, logfile):
"""Enables logging of SSH protocol output to given ``logfile``.
Expand All @@ -617,7 +630,8 @@ def enable_ssh_logging(self, logfile):

def open_connection(self, host, alias=None, port=22, timeout=None,
newline=None, prompt=None, term_type=None, width=None,
height=None, path_separator=None, encoding=None, escape_ansi=None, encoding_errors=None):
height=None, path_separator=None, encoding=None, escape_ansi=None, encoding_errors=None,
socket_timeout=None):
"""Opens a new SSH connection to the given ``host`` and ``port``.

The new connection is made active. Possible existing connections
Expand Down Expand Up @@ -691,8 +705,10 @@ def open_connection(self, host, alias=None, port=22, timeout=None,
encoding = encoding or self._config.encoding
escape_ansi = escape_ansi or self._config.escape_ansi
encoding_errors = encoding_errors or self._config.encoding_errors
socket_timeout = socket_timeout or self._config.socket_timeout
client = SSHClient(host, alias, port, timeout, newline, prompt,
term_type, width, height, path_separator, encoding, escape_ansi, encoding_errors)
term_type, width, height, path_separator, encoding, escape_ansi, encoding_errors,
socket_timeout)
connection_index = self._connections.register(client, alias)
client.config.update(index=connection_index)
return connection_index
Expand Down Expand Up @@ -1954,7 +1970,8 @@ def list_directories_in_directory(self, path, pattern=None, absolute=False):
class _DefaultConfiguration(Configuration):

def __init__(self, timeout, newline, prompt, loglevel, term_type, width,
height, path_separator, encoding, escape_ansi, encoding_errors):
height, path_separator, encoding, escape_ansi, encoding_errors,
socket_timeout):
super(_DefaultConfiguration, self).__init__(
timeout=TimeEntry(timeout),
newline=NewlineEntry(newline),
Expand All @@ -1966,5 +1983,6 @@ def __init__(self, timeout, newline, prompt, loglevel, term_type, width,
path_separator=StringEntry(path_separator),
encoding=StringEntry(encoding),
escape_ansi=StringEntry(escape_ansi),
encoding_errors=StringEntry(encoding_errors)
encoding_errors=StringEntry(encoding_errors),
socket_timeout=IntegerEntry(socket_timeout)
)
6 changes: 3 additions & 3 deletions src/SSHLibrary/pythonclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def _create_scp_transfer_client(self):
return SCPTransferClient(self.client, self.config.encoding)

def _create_scp_all_client(self):
return SCPClient(self.client)
return SCPClient(self.client, self.config.socket_timeout)

def _create_shell(self):
return Shell(self.client, self.config.term_type,
Expand Down Expand Up @@ -390,8 +390,8 @@ def _readlink(self, path):


class SCPClient(object):
def __init__(self, ssh_client):
self._scp_client = scp.SCPClient(ssh_client.get_transport())
def __init__(self, ssh_client, socket_timeout):
self._scp_client = scp.SCPClient(ssh_client.get_transport(), socket_timeout=socket_timeout)

def put_file(self, source, destination, scp_preserve_times, *args):
sources = self._get_put_file_sources(source)
Expand Down