Skip to content

Commit 0888205

Browse files
Sergey Chipigalukeis
Sergey Chipiga
authored andcommitted
support socket timeout for connections
add getter and setter for remoteconnection timeout Signed-off-by: Luke Inman-Semerau <[email protected]>
1 parent 01f54d2 commit 0888205

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

py/selenium/webdriver/remote/remote_connection.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ class RemoteConnection(object):
133133
134134
Communicates with the server using the WebDriver wire protocol:
135135
https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol"""
136+
137+
_timeout = socket._GLOBAL_DEFAULT_TIMEOUT
138+
139+
@classmethod
140+
def get_timeout(cls):
141+
return None if cls._timeout == socket._GLOBAL_DEFAULT_TIMEOUT or cls._timeout
142+
143+
@classmethod
144+
def set_timeout(cls, timeout):
145+
cls._timeout = timeout
146+
147+
@classmethod
148+
def reset_timeout(cls):
149+
cls._timeout = socket._GLOBAL_DEFAULT_TIMEOUT
150+
136151
def __init__(self, remote_server_addr, keep_alive=False):
137152
# Attempt to resolve the hostname and get an IP address.
138153
self.keep_alive = keep_alive
@@ -157,7 +172,9 @@ def __init__(self, remote_server_addr, keep_alive=False):
157172

158173
self._url = remote_server_addr
159174
if keep_alive:
160-
self._conn = httplib.HTTPConnection(str(addr), str(parsed_url.port))
175+
self._conn = httplib.HTTPConnection(
176+
str(addr), str(parsed_url.port), timeout=self._timeout)
177+
161178
self._commands = {
162179
Command.STATUS: ('GET', '/status'),
163180
Command.NEW_SESSION: ('POST', '/session'),
@@ -386,7 +403,7 @@ def _request(self, method, url, body=None):
386403
try:
387404
self._conn.request(method, parsed_url.path, body, headers)
388405
resp = self._conn.getresponse()
389-
except httplib.HTTPException:
406+
except (httplib.HTTPException, socket.error):
390407
self._conn.close()
391408
raise
392409

@@ -422,7 +439,7 @@ def _request(self, method, url, body=None):
422439
else:
423440
opener = url_request.build_opener(url_request.HTTPRedirectHandler(),
424441
HttpErrorHandler())
425-
resp = opener.open(request)
442+
resp = opener.open(request, timeout=self._timeout)
426443
statuscode = resp.code
427444
if not hasattr(resp, 'getheader'):
428445
if hasattr(resp.headers, 'getheader'):

0 commit comments

Comments
 (0)