Skip to content

Commit

Permalink
Overcome "Connection Refused" on some operations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dagwieers committed Aug 20, 2017
1 parent b1a54bc commit db3c89e
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion winrm/transport.py
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -189,7 +191,17 @@ def send_message(self, message):
prepared_request = self.session.prepare_request(request)

try:
response = self.session.send(prepared_request, timeout=self.read_timeout_sec)

# Retry connection on 'Connection refused'
for attempt in range(5):
try:
response = self.session.send(prepared_request, timeout=self.read_timeout_sec)
break
except requests.exceptions.ConnectionError as e:
if attempt == 4 or 'connection refused' not in str(e).lower():
raise
time.sleep(5)

response_text = response.text
response.raise_for_status()
return response_text
Expand Down

0 comments on commit db3c89e

Please sign in to comment.