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

Make SSH_TIMEOUT an overridable argument #156

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 3 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
16 changes: 13 additions & 3 deletions sshtunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
TRACE_LEVEL = 1
_CONNECTION_COUNTER = 1
_LOCK = threading.Lock()
#: Timeout (seconds) for the connection to the SSH gateway, ``None`` to disable
SSH_TIMEOUT = None
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that someone uses code like so:

import sshtunnel
import threading
import time

# incompatible !
sshtunnel.SSH_TIMEOUT = 3

tunnel = sshtunnel.SSHTunnelForwarder(
            ('172.18.19.20', 22),
            ssh_username='user',
            ssh_password='password',
            remote_bind_address=('192.168.1.10', 24),
            set_keepalive=0.5
        )

It's already a part of a public API :(

You can just use it as default for gateway_timeout

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pahaz

I hadn't considered that. Okay, that's fixable.

What do you think about the following?

  1. Leave SSH_TIMEOUT where it is.
#: Timeout (seconds) for the connection to the SSH gateway, ``None`` to disable
SSH_TIMEOUT = None
  1. In the constructor for SSHTunnelForwarder, set self.gateway_timeout based upon both the argument and the global value:
self.gateway_timeout = gateway_timeout if gateway_timeout is not None else SSH_TIMEOUT

In the public API, there would be two ways to set the timeouts. One would be global, and the other would be for a single instance. If you set both timeouts, the setting on the specific instance is used.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's ok

DEPRECATIONS = {
'ssh_address': 'ssh_address_or_host',
'ssh_host': 'ssh_address_or_host',
Expand Down Expand Up @@ -644,6 +642,16 @@ class SSHTunnelForwarder(object):

.. versionadded:: 0.0.8


gateway_timeout (float):
Time in seconds defining the period after which, if no tunnel
has been created, the connection attempt is stopped and a
:class:`BaseSSHTunnelForwarderError` is raised.

Default: 30.0 (gateway must be brought up within 30 seconds)

.. versionadded:: 0.1.3

set_keepalive (float):
Interval in seconds defining the period in which, if no data
was sent over the connection, a *'keepalive'* packet will be
Expand Down Expand Up @@ -825,6 +833,7 @@ def __init__(
mute_exceptions=False,
remote_bind_address=None,
remote_bind_addresses=None,
gateway_timeout=30.0,
set_keepalive=0.0,
threaded=True, # old version False
compression=None,
Expand All @@ -840,6 +849,7 @@ def __init__(

self.ssh_host_key = ssh_host_key
self.set_keepalive = set_keepalive
self.gateway_timeout = gateway_timeout
self._server_list = [] # reset server list
self.tunnel_is_up = {} # handle tunnel status
self._threaded = threaded
Expand Down Expand Up @@ -1113,7 +1123,7 @@ def _get_transport(self):
else:
_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if isinstance(_socket, socket.socket):
_socket.settimeout(SSH_TIMEOUT)
_socket.settimeout(self.gateway_timeout)
_socket.connect((self.ssh_host, self.ssh_port))
transport = paramiko.Transport(_socket)
transport.set_keepalive(self.set_keepalive)
Expand Down