Skip to content

Commit bb250ee

Browse files
committed
Simplify and restore order
1 parent e503e49 commit bb250ee

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

winrm/protocol.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Protocol(object):
2626
DEFAULT_OPERATION_TIMEOUT_SEC = 20
2727
DEFAULT_MAX_ENV_SIZE = 153600
2828
DEFAULT_LOCALE = 'en-US'
29+
DEFAULT_RECONNECTION_RETRIES = 5
30+
DEFAULT_RECONNECTION_SLEEP = 5
2931

3032
def __init__(
3133
self, endpoint, transport='plaintext', username=None,
@@ -39,6 +41,8 @@ def __init__(
3941
message_encryption='auto',
4042
credssp_disable_tlsv1_2=False,
4143
send_cbt=True,
44+
reconnection_retries = DEFAULT_RECONNECTION_RETRIES,
45+
reconnection_sleep = DEFAULT_RECONNECTION_SLEEP,
4246
):
4347
"""
4448
@param string endpoint: the WinRM webservice endpoint
@@ -57,6 +61,8 @@ def __init__(
5761
@param int operation_timeout_sec: maximum allowed time in seconds for any single wsman HTTP operation (default 20). Note that operation timeouts while receiving output (the only wsman operation that should take any significant time, and where these timeouts are expected) will be silently retried indefinitely. # NOQA
5862
@param string kerberos_hostname_override: the hostname to use for the kerberos exchange (defaults to the hostname in the endpoint URL)
5963
@param bool message_encryption_enabled: Will encrypt the WinRM messages if set to True and the transport auth supports message encryption (Default True).
64+
@param int reconnection_retries: Number of retries on Connection Refused
65+
@param int reconnection_sleep: Number of seconds to sleep between reconnection attempts
6066
"""
6167

6268
try:
@@ -88,7 +94,9 @@ def __init__(
8894
auth_method=transport,
8995
message_encryption=message_encryption,
9096
credssp_disable_tlsv1_2=credssp_disable_tlsv1_2,
91-
send_cbt=send_cbt
97+
send_cbt=send_cbt,
98+
reconnection_retries=reconnection_retries,
99+
reconnection_sleep=reconnection_sleep,
92100
)
93101

94102
self.username = username
@@ -100,6 +108,8 @@ def __init__(
100108
self.kerberos_delegation = kerberos_delegation
101109
self.kerberos_hostname_override = kerberos_hostname_override
102110
self.credssp_disable_tlsv1_2 = credssp_disable_tlsv1_2
111+
self.reconnection_retries = reconnection_retries
112+
self.reconnection_sleep = reconnection_sleep
103113

104114
def open_shell(self, i_stream='stdin', o_stream='stdout stderr',
105115
working_directory=None, env_vars=None, noprofile=False,

winrm/transport.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from __future__ import unicode_literals
2-
import errno
32
import inspect
43
import os
54
import sys
@@ -64,7 +63,10 @@ def __init__(
6463
auth_method='auto',
6564
message_encryption='auto',
6665
credssp_disable_tlsv1_2=False,
67-
send_cbt=True):
66+
send_cbt=True,
67+
reconnection_retries=5,
68+
reconnection_sleep=5,
69+
):
6870
self.endpoint = endpoint
6971
self.username = username
7072
self.password = password
@@ -80,6 +82,8 @@ def __init__(
8082
self.message_encryption = message_encryption
8183
self.credssp_disable_tlsv1_2 = credssp_disable_tlsv1_2
8284
self.send_cbt = send_cbt
85+
self.reconnection_retries = reconnection_retries
86+
self.reconnection_sleep = reconnection_sleep
8387

8488
if self.server_cert_validation not in [None, 'validate', 'ignore']:
8589
raise WinRMError('invalid server_cert_validation mode: %s' % self.server_cert_validation)
@@ -115,7 +119,7 @@ def __init__(
115119
from requests.packages.urllib3.exceptions import InsecureRequestWarning
116120
warnings.simplefilter('ignore', category=InsecureRequestWarning)
117121
except: pass # oh well, we tried...
118-
122+
119123
try:
120124
from urllib3.exceptions import InsecureRequestWarning
121125
warnings.simplefilter('ignore', category=InsecureRequestWarning)
@@ -264,14 +268,17 @@ def _send_message_request(self, prepared_request, message):
264268
try:
265269

266270
# Retry connection on 'Connection refused'
267-
for attempt in range(5):
271+
for attempt in range(self.reconnection_retries):
268272
try:
269273
response = self.session.send(prepared_request, timeout=self.read_timeout_sec)
274+
except requests.packages.urllib3.exceptions.NewConnectionError as e:
275+
time.sleep(self.reconnection_sleep)
276+
# except requests.exceptions.ConnectionError as e:
277+
# if attempt == 4 or 'connection refused' not in str(e).lower():
278+
# raise
279+
# time.sleep(self.reconnection_sleep)
280+
else:
270281
break
271-
except requests.exceptions.ConnectionError as e:
272-
if attempt == 4 or 'connection refused' not in str(e).lower():
273-
raise
274-
time.sleep(5)
275282

276283
response.raise_for_status()
277284
return response
@@ -283,7 +290,6 @@ def _send_message_request(self, prepared_request, message):
283290
else:
284291
response_text = ''
285292

286-
287293
raise WinRMTransportError('http', ex.response.status_code, response_text)
288294

289295
def _get_message_response_text(self, response):

0 commit comments

Comments
 (0)