From 272716af4163f76fcc79c21d9f55ba479203a0a6 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Sat, 7 Jan 2017 16:33:48 +0100 Subject: [PATCH] Overcome "Connection Refused" on some operations So some operations on Windows actually cause the WinRM service to become unavailable for some time and WinRM currently cannot overcome this problem. Since this is a real problem to us when e.g. installing SCVMM on servers and we cannot influence the installer, here is the work-around we implemented. The current implementation simply tries 5 times with a 5 seconds delay, but YMMV. Ideally this is configurable by the user, and future defaults should be tested. --- winrm/transport.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/winrm/transport.py b/winrm/transport.py index bb2f881a..5c3623dd 100644 --- a/winrm/transport.py +++ b/winrm/transport.py @@ -1,8 +1,10 @@ from __future__ import unicode_literals from contextlib import contextmanager +import errno import re import sys import os +import time import weakref is_py2 = sys.version[0] == '2' @@ -189,7 +191,18 @@ def send_message(self, message): prepared_request = self.session.prepare_request(request) try: - response = self.session.send(prepared_request, timeout=self.read_timeout_sec) + + for retry in range(5): + try: + response = self.session.send(prepared_request, timeout=self.read_timeout_sec) + break + except requests.exceptions.ConnectionError as e: + if e.args[0].reason.errno != errno.ECONNREFUSED: + raise + if retry == 4: + raise + time.sleep(5) + response_text = response.text response.raise_for_status() return response_text